tinywebd

diff src/http.c @ 7:5ec50ca0d071

separated the server code
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 17 Apr 2015 11:45:08 +0300
parents def49a046566
children
line diff
     1.1 --- a/src/http.c	Fri Apr 17 01:57:25 2015 +0300
     1.2 +++ b/src/http.c	Fri Apr 17 11:45:08 2015 +0300
     1.3 @@ -5,9 +5,10 @@
     1.4  #include <ctype.h>
     1.5  #include <alloca.h>
     1.6  #include "http.h"
     1.7 +#include "logger.h"
     1.8  
     1.9  
    1.10 -const char *http_method_str[] = {
    1.11 +static const char *http_method_str[] = {
    1.12  	"<unknown>",
    1.13  	"OPTIONS",
    1.14  	"GET",
    1.15 @@ -22,13 +23,13 @@
    1.16  
    1.17  
    1.18  /* HTTP 1xx message strings */
    1.19 -const char *http_msg1xx[] = {
    1.20 +static const char *http_msg1xx[] = {
    1.21  	"Continue",					/* 100 */
    1.22  	"Switching Protocols"		/* 101 */
    1.23  };
    1.24  
    1.25  /* HTTP 2xx message strings */
    1.26 -const char *http_msg2xx[] = {
    1.27 +static const char *http_msg2xx[] = {
    1.28  	"OK",						/* 200 */
    1.29  	"Created",					/* 201 */
    1.30  	"Accepted",					/* 202 */
    1.31 @@ -39,7 +40,7 @@
    1.32  };
    1.33  
    1.34  /* HTTP 3xx message strings */
    1.35 -const char *http_msg3xx[] = {
    1.36 +static const char *http_msg3xx[] = {
    1.37  	"Multiple Choices",			/* 300 */
    1.38  	"Moved Permanently",		/* 301 */
    1.39  	"Found",					/* 302 */
    1.40 @@ -51,7 +52,7 @@
    1.41  };
    1.42  
    1.43  /* HTTP 4xx error strings */
    1.44 -const char *http_msg4xx[] = {
    1.45 +static const char *http_msg4xx[] = {
    1.46  	"Bad Request",				/* 400 */
    1.47  	"Unauthorized",				/* 401 */
    1.48  	"What the Fuck?",			/* 402 */
    1.49 @@ -73,7 +74,7 @@
    1.50  };
    1.51  
    1.52  /* HTTP 5xx error strings */
    1.53 -const char *http_msg5xx[] = {
    1.54 +static const char *http_msg5xx[] = {
    1.55  	"Internal Server Error",	/* 500 */
    1.56  	"Not Implemented",			/* 501 */
    1.57  	"Bad Gateway",				/* 502 */
    1.58 @@ -86,7 +87,7 @@
    1.59  static enum http_method parse_method(const char *s);
    1.60  
    1.61  
    1.62 -int http_parse_header(struct http_req_header *hdr, const char *buf, int bufsz)
    1.63 +int http_parse_request(struct http_req_header *hdr, const char *buf, int bufsz)
    1.64  {
    1.65  	int i, nlines = 0;
    1.66  	char *rqline = 0;
    1.67 @@ -167,23 +168,23 @@
    1.68  	return HTTP_HDR_OK;
    1.69  }
    1.70  
    1.71 -void http_print_header(struct http_req_header *hdr)
    1.72 +void http_log_request(struct http_req_header *hdr)
    1.73  {
    1.74  	int i;
    1.75  
    1.76 -	printf("HTTP request header\n");
    1.77 -	printf(" method: %s\n", http_method_str[hdr->method]);
    1.78 -	printf(" uri: %s\n", hdr->uri);
    1.79 -	printf(" version: %d.%d\n", hdr->ver_major, hdr->ver_minor);
    1.80 -	printf(" fields (%d):\n", hdr->num_hdrfields);
    1.81 +	logmsg("HTTP request header\n");
    1.82 +	logmsg(" method: %s\n", http_method_str[hdr->method]);
    1.83 +	logmsg(" uri: %s\n", hdr->uri);
    1.84 +	logmsg(" version: %d.%d\n", hdr->ver_major, hdr->ver_minor);
    1.85 +	logmsg(" fields (%d):\n", hdr->num_hdrfields);
    1.86  
    1.87  	for(i=0; i<hdr->num_hdrfields; i++) {
    1.88 -		printf("   %s\n", hdr->hdrfields[i]);
    1.89 +		logmsg("   %s\n", hdr->hdrfields[i]);
    1.90  	}
    1.91 -	putchar('\n');
    1.92 +	logmsg("\n");
    1.93  }
    1.94  
    1.95 -void http_destroy_header(struct http_req_header *hdr)
    1.96 +void http_destroy_request(struct http_req_header *hdr)
    1.97  {
    1.98  	int i;
    1.99  
   1.100 @@ -211,7 +212,7 @@
   1.101  {
   1.102  	int sz;
   1.103  	va_list ap;
   1.104 -	char *field, *newarr, tmp;
   1.105 +	char *field, **newarr, tmp;
   1.106  
   1.107  	va_start(ap, fmt);
   1.108  	sz = vsnprintf(&tmp, 0, fmt, ap);
   1.109 @@ -229,7 +230,9 @@
   1.110  		free(field);
   1.111  		return -1;
   1.112  	}
   1.113 -	resp->fields[resp->num_fields++] = newarr;
   1.114 +	resp->fields = newarr;
   1.115 +
   1.116 +	resp->fields[resp->num_fields++] = field;
   1.117  	return 0;
   1.118  }
   1.119