hermes

HTTP GET/HEAD-only web server for static content.
git clone git://git.francescosaccone.com/hermes
Log | Files | Refs | README | LICENSE

http.h (1853B)


      1 #ifndef HTTP_H
      2 #define HTTP_H
      3 
      4 #define HTTP_REQUEST_PATH_MAX_LENGTH 256
      5 
      6 enum http_request_method {
      7 	GET,
      8 	HEAD,
      9 	UNSUPPORTED,
     10 };
     11 
     12 enum http_response_status {
     13 	BAD_REQUEST,
     14 	FORBIDDEN,
     15 	INTERNAL_SERVER_ERROR,
     16 	METHOD_NOT_ALLOWED,
     17 	MOVED_PERMANENTLY,
     18 	NOT_FOUND,
     19 	NOT_MODIFIED,
     20 	OK,
     21 	PARTIAL_CONTENT,
     22 	RANGE_NOT_SATISFIABLE,
     23 	REQUEST_TIMEOUT,
     24 	REQUEST_TOO_LARGE,
     25 	VERSION_NOT_SUPPORTED,
     26 };
     27 
     28 static const struct {
     29 	enum http_response_status status;
     30 	unsigned int code;
     31 	const char *message;
     32 } status_map[] = {
     33 	{ BAD_REQUEST          , 400, "Bad Request" },
     34 	{ FORBIDDEN            , 403, "Forbidden" },
     35 	{ INTERNAL_SERVER_ERROR, 500, "Internal Server Error" },
     36 	{ METHOD_NOT_ALLOWED   , 405, "Method Not Allowed" },
     37 	{ MOVED_PERMANENTLY    , 301, "Moved Permanently" },
     38 	{ NOT_FOUND            , 404, "Not Found" },
     39 	{ NOT_MODIFIED         , 304, "Not Modified" },
     40 	{ OK                   , 200, "OK" },
     41 	{ PARTIAL_CONTENT      , 206, "Partial Content" },
     42 	{ RANGE_NOT_SATISFIABLE, 416, "Range Not Satisfiable" },
     43 	{ REQUEST_TIMEOUT      , 408, "Request Timeout" },
     44 	{ REQUEST_TOO_LARGE    , 413, "Request Entity Too Large" },
     45 	{ VERSION_NOT_SUPPORTED, 505, "HTTP Version Not Supported" },
     46 };
     47 
     48 struct http_request {
     49 	enum http_request_method method;
     50 	char path[HTTP_REQUEST_PATH_MAX_LENGTH];
     51 };
     52 
     53 struct http_response {
     54 	enum http_response_status status;
     55 	const char *content_type;
     56 	char *body;
     57 };
     58 
     59 /*
     60  * Returns the http_request from a raw client request string.
     61  */
     62 struct http_request *
     63 parse_http_request(char *request);
     64 
     65 /*
     66  * Returns the head of the raw HTTP response from a given a http_response.
     67  */
     68 char *
     69 compose_http_response_head(struct http_response response);
     70 
     71 /*
     72  * Returns the raw HTTP response, composed of both head and body, from a
     73  * given a http_response.
     74  */
     75 char *
     76 compose_http_response_full(struct http_response response);
     77 
     78 #endif