HTTP Toolkit |
Hyper Text Transfer Protocol (HTTP) is by far the most widely used means for communication of information. Daylight is leveraging HTTP by developing a simple, flexible, and robust way of communicating chemical information over the internet. The HTTP Toolkit provides the means to deliver all Daylight functionality, including DayCart, through a browser. The Application Programmer Interface (API) of the toolkit will be introduced and examples of its use will be shown.
Now, let's use the HTTP Toolkit API to make some programs.
int main() { dt_Handle http, www; http = dt_alloc_http(80); for (;;) { www = dt_http_get(http); dt_http_put(http,www); dt_dealloc(www); } return 0; }
The above example is a "bare bones" program that will always return something like:
Not Found
The requested URL /homepage.html was not found on this server.
Daylight/4.81 Server at http.daylight.com Port 80
For completeness, let's improve upon the above example by including error checking, deallocation of the "http" object, and some comments.
int main()
{
dt_Handle http, www; /* HTTP and get/put objects */
/* create a web server on port 80 */
http = dt_alloc_http(80);
/* loop forever until broken herein */
for (;;)
{
/* receive data */
if (NULL_OB == (www = dt_http_get(http)))
continue;
/* send response */
if (FALSE == dt_http_put(http,www))
break;
/* free object */
if (FALSE == dt_dealloc(www))
break;
}
/* free web server */
dt_dealloc(http);
/* exit value */
return 1;
}
This program will behave like the first one, except it will deallocate the "http" and terminate if a call to dt_http_put() or dt_dealloc() fails.
Named properties associate a keyword/value pair with a HTTP object. The most important thing to know about an HTTP process is what happened while it was receiving data from a port or CGI. The "http_status" keyword is used to indicated what happened when receiving data. Possible values are 1) data was received, 2) data was not received, 3) an error occured, and 4) a termination condition was met.
Then, you can use the "http_reply" and "http_mime" properties to respond to a request. Now, let's change our "simpliest example" from above and use named properties to respond with the text message "OK" when data is received.
int main() { dt_Handle http, www; /* HTTP and get/put objects */ /* create a web server on port 80 */ http = dt_alloc_http(80); /* loop forever until broken herein */ for (;;) { /* receive data */ if (NULL_OB == (www = dt_http_get(http))) continue; /* set response and mime type */ dt_setstring(www, 10, "http_reply", 2, "OK"); dt_setstring(www, 9, "http_mime", 9, "text/html"); /* send response */ if (FALSE == dt_http_put(http, www)) break; /* free object */ if (FALSE == dt_dealloc(www)) break; } /* free web server */ dt_dealloc(http); /* exit value */ return 1; }Now, let's expand our program to check for the four types of status and log the activity of the process.
int main() { dt_Handle http, www; /* HTTP and get/put objects */ dt_Integer status; /* state of data handling */ /* create a web server on port 80 */ http = dt_alloc_http(80); /* loop forever until broken herein */ for (;;) { /* receive data */ www = dt_http_get(http); /* get status */ status = dt_integer(www, 11, "http_status"); /* handle status */ switch (status) { /* data was received */ case DX_HTTP_OK: /* set response and mime type */ dt_setstring(www, 10, "http_reply", 2, "OK"); dt_setstring(www, 9, "http_mime", 9, "text/html"); /* send response */ if (FALSE == dt_http_put(http, www)) status = DX_HTTP_DONE; /* free object */ if (FALSE == dt_dealloc(www)) status = DX_HTTP_DONE; /* logging */ fprintf (stderr, "OK\n"); break; /* data was not received */ case DX_HTTP_TIMEOUT: /* logging */ fprintf(stderr, "timeout\n"); break; /* an error occured, i.e., an interupt or a communication failure */ case DX_HTTP_ERROR: /* logging */ fprintf(stderr, "error\n"); break; /* a termination was met */ case DX_HTTP_DONE: /* logging */ fprintf(stderr, "done\n"); break; /* trap unknown states */ default: /* logging */ fprintf(stderr, "unknown\n"); break; } /* loop under the following conditions */ if ((DX_HTTP_OK != status) && (DX_HTTP_TIMEOUT != status)) break; } /* logging */ fprintf (stderr, "exit %d\n", status); /* free web server */ dt_dealloc(http); /* exit value */ return status; }Here are all of the named properties.