quark

quark web server
git clone git://git.suckless.org/quark
Log | Files | Refs | LICENSE

http.h (2232B)


      1 /* See LICENSE file for copyright and license details. */
      2 #ifndef HTTP_H
      3 #define HTTP_H
      4 
      5 #include <limits.h>
      6 #include <sys/socket.h>
      7 
      8 #include "config.h"
      9 #include "util.h"
     10 
     11 enum req_field {
     12 	REQ_HOST,
     13 	REQ_RANGE,
     14 	REQ_IF_MODIFIED_SINCE,
     15 	NUM_REQ_FIELDS,
     16 };
     17 
     18 extern const char *req_field_str[];
     19 
     20 enum req_method {
     21 	M_GET,
     22 	M_HEAD,
     23 	NUM_REQ_METHODS,
     24 };
     25 
     26 extern const char *req_method_str[];
     27 
     28 struct request {
     29 	enum req_method method;
     30 	char uri[PATH_MAX];
     31 	char field[NUM_REQ_FIELDS][FIELD_MAX];
     32 };
     33 
     34 enum status {
     35 	S_OK                    = 200,
     36 	S_PARTIAL_CONTENT       = 206,
     37 	S_MOVED_PERMANENTLY     = 301,
     38 	S_NOT_MODIFIED          = 304,
     39 	S_BAD_REQUEST           = 400,
     40 	S_FORBIDDEN             = 403,
     41 	S_NOT_FOUND             = 404,
     42 	S_METHOD_NOT_ALLOWED    = 405,
     43 	S_REQUEST_TIMEOUT       = 408,
     44 	S_RANGE_NOT_SATISFIABLE = 416,
     45 	S_REQUEST_TOO_LARGE     = 431,
     46 	S_INTERNAL_SERVER_ERROR = 500,
     47 	S_VERSION_NOT_SUPPORTED = 505,
     48 };
     49 
     50 extern const char *status_str[];
     51 
     52 enum res_field {
     53 	RES_ACCEPT_RANGES,
     54 	RES_ALLOW,
     55 	RES_LOCATION,
     56 	RES_LAST_MODIFIED,
     57 	RES_CONTENT_LENGTH,
     58 	RES_CONTENT_RANGE,
     59 	RES_CONTENT_TYPE,
     60 	NUM_RES_FIELDS,
     61 };
     62 
     63 extern const char *res_field_str[];
     64 
     65 enum res_type {
     66 	RESTYPE_ERROR,
     67 	RESTYPE_FILE,
     68 	RESTYPE_DIRLISTING,
     69 	NUM_RES_TYPES,
     70 };
     71 
     72 struct response {
     73 	enum res_type type;
     74 	enum status status;
     75 	char field[NUM_RES_FIELDS][FIELD_MAX];
     76 	char uri[PATH_MAX];
     77 	char path[PATH_MAX];
     78 	struct {
     79 		size_t lower;
     80 		size_t upper;
     81 	} file;
     82 };
     83 
     84 enum conn_state {
     85 	C_VACANT,
     86 	C_RECV_HEADER,
     87 	C_SEND_HEADER,
     88 	C_SEND_BODY,
     89 	NUM_CONN_STATES,
     90 };
     91 
     92 struct connection {
     93 	enum conn_state state;
     94 	int fd;
     95 	struct sockaddr_storage ia;
     96 	struct request req;
     97 	struct response res;
     98 	struct buffer buf;
     99 	size_t progress;
    100 };
    101 
    102 enum status http_prepare_header_buf(const struct response *, struct buffer *);
    103 enum status http_send_buf(int, struct buffer *);
    104 enum status http_recv_header(int, struct buffer *, int *);
    105 enum status http_parse_header(const char *, struct request *);
    106 void http_prepare_response(const struct request *, struct response *,
    107                            const struct server *);
    108 void http_prepare_error_response(const struct request *,
    109                                  struct response *, enum status);
    110 
    111 #endif /* HTTP_H */