Http parsing and responses are now mostly complete

This commit is contained in:
2014-07-25 17:23:38 +01:00
parent bfe3a66903
commit a97b541174
14 changed files with 2865 additions and 114 deletions

54
content/lorem.txt Normal file
View File

@@ -0,0 +1,54 @@
Lorem ipsum dolor sit amet, quo at porro hendrerit, ius iudico omittam
dissentiunt at. Habeo reprimique id pro, case populo te usu. Id duo doming
evertitur. Id sed fuisset complectitur, eam an quando phaedrum theophrastus,
alii placerat senserit ex vel. Vim congue appareat mediocrem id, postea
accumsan ea mea. Cum ex tation feugiat nominati, nec id regione posidonium, at
menandri sapientem sit.
Ad rebum assentior adversarium eos, libris aliquid voluptaria ei his. Ex nobis
accommodare eos. Et ridens bonorum democritum pro, per ad antiopam
comprehensam. Brute dolore erroribus no sit, pro legere contentiones ei. Ut
debet fierent mediocritatem quo.
Mei alterum assueverit ut, ne feugait referrentur quo. Partem postea blandit
mei an, at modo viris indoctum mei. In nam saepe vitae, tantas nonumes
salutatus eos ad. Mel possit verear conclusionemque ut.
Ius corpora accusata percipitur ea, mutat affert dicant has cu. Cu habeo
vivendo usu, mel mazim iriure ei. Te eum ignota semper suscipiantur. Porro
saepe insolens ex sed. Vis et dicit ceteros corrumpit, iudico iudicabit eam ex.
Aliquando gloriatur honestatis no ius.
Te vis vocent eruditi consetetur, vim erat theophrastus ea. Sed cu sensibus
vituperata efficiantur. Pri tantas laoreet an, vel perfecto voluptatum
complectitur no. Mei ex nulla maluisset, offendit oportere indoctum mea no.
Tollit probatus eu mei, duo ut magna dicat zril. Duo menandri signiferumque in,
dolores platonem id quo, ei minim splendide duo. Pri et omittam petentium, ut
vim consul nonumes, nibh reformidans deterruisset mei cu.
Per ut verear utamur honestatis, mel constituam comprehensam ea. Vidit paulo
hendrerit eu pri, nisl wisi partiendo vix ad, eum ad possim propriae petentium.
Quo ex illum vocibus officiis, ut ius nonumy nominavi. Per ut quando tantas
concludaturque, ex quo minim facilisis expetendis. Has in alia conceptam
interpretaris, summo falli virtute eum ex. Vel quodsi lobortis inimicus at,
summo mundi putant pri ne, ut qui utroque singulis.
Eos lucilius concludaturque no, vix libris menandri argumentum ad. Cu vis unum
graeci dicunt, alterum consulatu eu vix, ei est iudico tractatos. Eu eam dicam
vocibus. Quo an mutat dissentiunt, id sea quando consul laudem, sea ne inani
denique. Ex vix labores efficiantur, duo vero etiam oportere te. Has in
nominati interesset, id purto ceteros molestie duo.
Postea offendit efficiendi te ius, ne sea dictas delicatissimi. Te exerci
tempor admodum per, est in enim errem ignota. Sed dico partem omnium ut. At vix
suavitate ullamcorper disputationi. Vim praesent tractatos an, cum liber
aliquam perpetua ex.
Usu lorem audire cu. Has cu illud summo aliquid. No his saepe impetus
euripidis. Vocent aperiam suavitate eam no. His feugait explicari te, eam etiam
graece ullamcorper et. Duo nulla eleifend at.
Ea sit aliquam vocibus. Ex eos alterum inermis, an vel viris oblique, no
vivendum adolescens est. Per augue movet no, labores splendide eos id, eu per
mazim quando legendos. No accusam vulputate duo, ius aliquam repudiandae
efficiantur no. Fugit option indoctum ut sit, ex enim indoctum pro.

2259
lib/http_parser.c Normal file

File diff suppressed because it is too large Load Diff

327
lib/http_parser.h Normal file
View File

@@ -0,0 +1,327 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef http_parser_h
#define http_parser_h
#ifdef __cplusplus
extern "C" {
#endif
/* Also update SONAME in the Makefile whenever you change these. */
#define HTTP_PARSER_VERSION_MAJOR 2
#define HTTP_PARSER_VERSION_MINOR 3
#define HTTP_PARSER_VERSION_PATCH 0
#include <sys/types.h>
#if defined(_WIN32) && !defined(__MINGW32__) && (!defined(_MSC_VER) || _MSC_VER<1600)
#include <BaseTsd.h>
#include <stddef.h>
typedef __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#else
#include <stdint.h>
#endif
/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
* faster
*/
#ifndef HTTP_PARSER_STRICT
# define HTTP_PARSER_STRICT 1
#endif
/* Maximium header size allowed. If the macro is not defined
* before including this header then the default is used. To
* change the maximum header size, define the macro in the build
* environment (e.g. -DHTTP_MAX_HEADER_SIZE=<value>). To remove
* the effective limit on the size of the header, define the macro
* to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff)
*/
#ifndef HTTP_MAX_HEADER_SIZE
# define HTTP_MAX_HEADER_SIZE (80*1024)
#endif
typedef struct http_parser http_parser;
typedef struct http_parser_settings http_parser_settings;
/* Callbacks should return non-zero to indicate an error. The parser will
* then halt execution.
*
* The one exception is on_headers_complete. In a HTTP_RESPONSE parser
* returning '1' from on_headers_complete will tell the parser that it
* should not expect a body. This is used when receiving a response to a
* HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
* chunked' headers that indicate the presence of a body.
*
* http_data_cb does not return data chunks. It will be call arbitrarally
* many times for each string. E.G. you might get 10 callbacks for "on_url"
* each providing just a few characters more data.
*/
typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
typedef int (*http_cb) (http_parser*);
/* Request Methods */
#define HTTP_METHOD_MAP(XX) \
XX(0, DELETE, DELETE) \
XX(1, GET, GET) \
XX(2, HEAD, HEAD) \
XX(3, POST, POST) \
XX(4, PUT, PUT) \
/* pathological */ \
XX(5, CONNECT, CONNECT) \
XX(6, OPTIONS, OPTIONS) \
XX(7, TRACE, TRACE) \
/* webdav */ \
XX(8, COPY, COPY) \
XX(9, LOCK, LOCK) \
XX(10, MKCOL, MKCOL) \
XX(11, MOVE, MOVE) \
XX(12, PROPFIND, PROPFIND) \
XX(13, PROPPATCH, PROPPATCH) \
XX(14, SEARCH, SEARCH) \
XX(15, UNLOCK, UNLOCK) \
/* subversion */ \
XX(16, REPORT, REPORT) \
XX(17, MKACTIVITY, MKACTIVITY) \
XX(18, CHECKOUT, CHECKOUT) \
XX(19, MERGE, MERGE) \
/* upnp */ \
XX(20, MSEARCH, M-SEARCH) \
XX(21, NOTIFY, NOTIFY) \
XX(22, SUBSCRIBE, SUBSCRIBE) \
XX(23, UNSUBSCRIBE, UNSUBSCRIBE) \
/* RFC-5789 */ \
XX(24, PATCH, PATCH) \
XX(25, PURGE, PURGE) \
/* CalDAV */ \
XX(26, MKCALENDAR, MKCALENDAR) \
enum http_method
{
#define XX(num, name, string) HTTP_##name = num,
HTTP_METHOD_MAP(XX)
#undef XX
};
enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
/* Flag values for http_parser.flags field */
enum flags
{ F_CHUNKED = 1 << 0
, F_CONNECTION_KEEP_ALIVE = 1 << 1
, F_CONNECTION_CLOSE = 1 << 2
, F_TRAILING = 1 << 3
, F_UPGRADE = 1 << 4
, F_SKIPBODY = 1 << 5
};
/* Map for errno-related constants
*
* The provided argument should be a macro that takes 2 arguments.
*/
#define HTTP_ERRNO_MAP(XX) \
/* No error */ \
XX(OK, "success") \
\
/* Callback-related errors */ \
XX(CB_message_begin, "the on_message_begin callback failed") \
XX(CB_url, "the on_url callback failed") \
XX(CB_header_field, "the on_header_field callback failed") \
XX(CB_header_value, "the on_header_value callback failed") \
XX(CB_headers_complete, "the on_headers_complete callback failed") \
XX(CB_body, "the on_body callback failed") \
XX(CB_message_complete, "the on_message_complete callback failed") \
XX(CB_status, "the on_status callback failed") \
\
/* Parsing-related errors */ \
XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
XX(HEADER_OVERFLOW, \
"too many header bytes seen; overflow detected") \
XX(CLOSED_CONNECTION, \
"data received after completed connection: close message") \
XX(INVALID_VERSION, "invalid HTTP version") \
XX(INVALID_STATUS, "invalid HTTP status code") \
XX(INVALID_METHOD, "invalid HTTP method") \
XX(INVALID_URL, "invalid URL") \
XX(INVALID_HOST, "invalid host") \
XX(INVALID_PORT, "invalid port") \
XX(INVALID_PATH, "invalid path") \
XX(INVALID_QUERY_STRING, "invalid query string") \
XX(INVALID_FRAGMENT, "invalid fragment") \
XX(LF_EXPECTED, "LF character expected") \
XX(INVALID_HEADER_TOKEN, "invalid character in header") \
XX(INVALID_CONTENT_LENGTH, \
"invalid character in content-length header") \
XX(INVALID_CHUNK_SIZE, \
"invalid character in chunk size header") \
XX(INVALID_CONSTANT, "invalid constant string") \
XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
XX(STRICT, "strict mode assertion failed") \
XX(PAUSED, "parser is paused") \
XX(UNKNOWN, "an unknown error occurred")
/* Define HPE_* values for each errno value above */
#define HTTP_ERRNO_GEN(n, s) HPE_##n,
enum http_errno {
HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
};
#undef HTTP_ERRNO_GEN
/* Get an http_errno value from an http_parser */
#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
struct http_parser {
/** PRIVATE **/
unsigned int type : 2; /* enum http_parser_type */
unsigned int flags : 6; /* F_* values from 'flags' enum; semi-public */
unsigned int state : 8; /* enum state from http_parser.c */
unsigned int header_state : 8; /* enum header_state from http_parser.c */
unsigned int index : 8; /* index into current matcher */
uint32_t nread; /* # bytes read in various scenarios */
uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
/** READ-ONLY **/
unsigned short http_major;
unsigned short http_minor;
unsigned int status_code : 16; /* responses only */
unsigned int method : 8; /* requests only */
unsigned int http_errno : 7;
/* 1 = Upgrade header was present and the parser has exited because of that.
* 0 = No upgrade header present.
* Should be checked when http_parser_execute() returns in addition to
* error checking.
*/
unsigned int upgrade : 1;
/** PUBLIC **/
void *data; /* A pointer to get hook to the "connection" or "socket" object */
};
struct http_parser_settings {
http_cb on_message_begin;
http_data_cb on_url;
http_data_cb on_status;
http_data_cb on_header_field;
http_data_cb on_header_value;
http_cb on_headers_complete;
http_data_cb on_body;
http_cb on_message_complete;
};
enum http_parser_url_fields
{ UF_SCHEMA = 0
, UF_HOST = 1
, UF_PORT = 2
, UF_PATH = 3
, UF_QUERY = 4
, UF_FRAGMENT = 5
, UF_USERINFO = 6
, UF_MAX = 7
};
/* Result structure for http_parser_parse_url().
*
* Callers should index into field_data[] with UF_* values iff field_set
* has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
* because we probably have padding left over), we convert any port to
* a uint16_t.
*/
struct http_parser_url {
uint16_t field_set; /* Bitmask of (1 << UF_*) values */
uint16_t port; /* Converted UF_PORT string */
struct {
uint16_t off; /* Offset into buffer in which field starts */
uint16_t len; /* Length of run in buffer */
} field_data[UF_MAX];
};
/* Returns the library version. Bits 16-23 contain the major version number,
* bits 8-15 the minor version number and bits 0-7 the patch level.
* Usage example:
*
* unsigned long version = http_parser_version();
* unsigned major = (version >> 16) & 255;
* unsigned minor = (version >> 8) & 255;
* unsigned patch = version & 255;
* printf("http_parser v%u.%u.%u\n", major, minor, version);
*/
unsigned long http_parser_version(void);
void http_parser_init(http_parser *parser, enum http_parser_type type);
size_t http_parser_execute(http_parser *parser,
const http_parser_settings *settings,
const char *data,
size_t len);
/* If http_should_keep_alive() in the on_headers_complete or
* on_message_complete callback returns 0, then this should be
* the last message on the connection.
* If you are the server, respond with the "Connection: close" header.
* If you are the client, close the connection.
*/
int http_should_keep_alive(const http_parser *parser);
/* Returns a string version of the HTTP method. */
const char *http_method_str(enum http_method m);
/* Return a string name of the given error */
const char *http_errno_name(enum http_errno err);
/* Return a string description of the given error */
const char *http_errno_description(enum http_errno err);
/* Parse a URL; return nonzero on failure */
int http_parser_parse_url(const char *buf, size_t buflen,
int is_connect,
struct http_parser_url *u);
/* Pause or un-pause the parser; a nonzero value pauses */
void http_parser_pause(http_parser *parser, int paused);
/* Checks if this is the final chunk of the body. */
int http_body_is_final(const http_parser *parser);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -35,6 +35,7 @@ OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}
# Object Files
OBJECTFILES= \
${OBJECTDIR}/lib/http_parser.o \
${OBJECTDIR}/src/http/http.o \
${OBJECTDIR}/src/http/parse.o \
${OBJECTDIR}/src/main.o \
@@ -55,7 +56,7 @@ FFLAGS=
ASFLAGS=
# Link Libraries and Options
LDLIBSOPTIONS=-lhttp_parser
LDLIBSOPTIONS=
# Build Targets
.build-conf: ${BUILD_SUBPROJECTS}
@@ -65,25 +66,30 @@ ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/khttp: ${OBJECTFILES}
${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}
${LINK.c} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/khttp ${OBJECTFILES} ${LDLIBSOPTIONS}
${OBJECTDIR}/lib/http_parser.o: lib/http_parser.c
${MKDIR} -p ${OBJECTDIR}/lib
${RM} "$@.d"
$(COMPILE.c) -g -Werror -Ilib -include lib/http_parser.h -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/lib/http_parser.o lib/http_parser.c
${OBJECTDIR}/src/http/http.o: src/http/http.c
${MKDIR} -p ${OBJECTDIR}/src/http
${RM} "$@.d"
$(COMPILE.c) -g -Werror -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/http/http.o src/http/http.c
$(COMPILE.c) -g -Werror -Ilib -include lib/http_parser.h -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/http/http.o src/http/http.c
${OBJECTDIR}/src/http/parse.o: src/http/parse.c
${MKDIR} -p ${OBJECTDIR}/src/http
${RM} "$@.d"
$(COMPILE.c) -g -Werror -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/http/parse.o src/http/parse.c
$(COMPILE.c) -g -Werror -Ilib -include lib/http_parser.h -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/http/parse.o src/http/parse.c
${OBJECTDIR}/src/main.o: src/main.c
${MKDIR} -p ${OBJECTDIR}/src
${RM} "$@.d"
$(COMPILE.c) -g -Werror -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/main.o src/main.c
$(COMPILE.c) -g -Werror -Ilib -include lib/http_parser.h -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/main.o src/main.c
${OBJECTDIR}/src/socket.o: src/socket.c
${MKDIR} -p ${OBJECTDIR}/src
${RM} "$@.d"
$(COMPILE.c) -g -Werror -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/socket.o src/socket.c
$(COMPILE.c) -g -Werror -Ilib -include lib/http_parser.h -std=c99 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/src/socket.o src/socket.c
# Subprojects
.build-subprojects:

View File

@@ -35,6 +35,7 @@ OBJECTDIR=${CND_BUILDDIR}/${CND_CONF}/${CND_PLATFORM}
# Object Files
OBJECTFILES= \
${OBJECTDIR}/lib/http_parser.o \
${OBJECTDIR}/src/http/http.o \
${OBJECTDIR}/src/http/parse.o \
${OBJECTDIR}/src/main.o \
@@ -65,6 +66,11 @@ ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/khttp: ${OBJECTFILES}
${MKDIR} -p ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}
${LINK.c} -o ${CND_DISTDIR}/${CND_CONF}/${CND_PLATFORM}/khttp ${OBJECTFILES} ${LDLIBSOPTIONS}
${OBJECTDIR}/lib/http_parser.o: lib/http_parser.c
${MKDIR} -p ${OBJECTDIR}/lib
${RM} "$@.d"
$(COMPILE.c) -O2 -MMD -MP -MF "$@.d" -o ${OBJECTDIR}/lib/http_parser.o lib/http_parser.c
${OBJECTDIR}/src/http/http.o: src/http/http.c
${MKDIR} -p ${OBJECTDIR}/src/http
${RM} "$@.d"

View File

@@ -5,6 +5,7 @@
displayName="Header Files"
projectFiles="true">
<itemPath>src/http/http.h</itemPath>
<itemPath>lib/http_parser.h</itemPath>
<itemPath>src/main.h</itemPath>
<itemPath>src/http/parse.h</itemPath>
<itemPath>src/socket.h</itemPath>
@@ -17,6 +18,7 @@
displayName="Source Files"
projectFiles="true">
<itemPath>src/http/http.c</itemPath>
<itemPath>lib/http_parser.c</itemPath>
<itemPath>src/main.c</itemPath>
<itemPath>src/http/parse.c</itemPath>
<itemPath>src/socket.c</itemPath>
@@ -33,10 +35,8 @@
<itemPath>Makefile</itemPath>
</logicalFolder>
<itemPath>content/error.html</itemPath>
<itemPath>content/lorem.txt</itemPath>
</logicalFolder>
<sourceRootList>
<Elem>include</Elem>
</sourceRootList>
<projectmakefile>Makefile</projectmakefile>
<confs>
<conf name="Debug" type="1">
@@ -48,17 +48,24 @@
<compileType>
<cTool>
<standard>3</standard>
<incDir>
<pElem>lib</pElem>
</incDir>
<incFile>
<pElem>lib/http_parser.h</pElem>
</incFile>
<commandLine>-O0</commandLine>
<warningLevel>3</warningLevel>
</cTool>
<linkerTool>
<linkerLibItems>
<linkerLibLibItem>http_parser</linkerLibLibItem>
</linkerLibItems>
</linkerTool>
</compileType>
<item path="content/error.html" ex="false" tool="3" flavor2="0">
</item>
<item path="content/lorem.txt" ex="false" tool="3" flavor2="0">
</item>
<item path="lib/http_parser.c" ex="false" tool="0" flavor2="0">
</item>
<item path="lib/http_parser.h" ex="false" tool="3" flavor2="0">
</item>
<item path="src/http/http.c" ex="false" tool="0" flavor2="0">
</item>
<item path="src/http/http.h" ex="false" tool="3" flavor2="0">
@@ -98,6 +105,12 @@
</compileType>
<item path="content/error.html" ex="false" tool="3" flavor2="0">
</item>
<item path="content/lorem.txt" ex="false" tool="3" flavor2="0">
</item>
<item path="lib/http_parser.c" ex="false" tool="0" flavor2="0">
</item>
<item path="lib/http_parser.h" ex="false" tool="3" flavor2="0">
</item>
<item path="src/http/http.c" ex="false" tool="0" flavor2="0">
</item>
<item path="src/http/http.h" ex="false" tool="3" flavor2="0">

View File

@@ -10,12 +10,13 @@
<file>file:/home/sam/NetBeansProjects/KHttp/src/socket.c</file>
<file>file:/home/sam/NetBeansProjects/KHttp/src/http/http.h</file>
<file>file:/home/sam/NetBeansProjects/KHttp/src/http/parse.c</file>
<file>file:/home/sam/NetBeansProjects/KHttp/src/ut/utarray.h</file>
<file>file:/home/sam/NetBeansProjects/KHttp/src/main.h</file>
<file>file:/home/sam/NetBeansProjects/KHttp/Makefile</file>
<file>file:/home/sam/NetBeansProjects/KHttp/lib/http_parser.h</file>
<file>file:/home/sam/NetBeansProjects/KHttp/content/error.html</file>
<file>file:/home/sam/NetBeansProjects/KHttp/src/http/parse.h</file>
<file>file:/home/sam/NetBeansProjects/KHttp/src/main.c</file>
<file>file:/home/sam/NetBeansProjects/KHttp/lib/http_parser.c</file>
<file>file:/home/sam/NetBeansProjects/KHttp/src/socket.h</file>
<file>file:/home/sam/NetBeansProjects/KHttp/src/http/http.c</file>
</group>

View File

@@ -9,9 +9,7 @@
<header-extensions>h</header-extensions>
<sourceEncoding>UTF-8</sourceEncoding>
<make-dep-projects/>
<sourceRootList>
<sourceRootElem>include</sourceRootElem>
</sourceRootList>
<sourceRootList/>
<confList>
<confElem>
<name>Debug</name>

View File

@@ -9,17 +9,16 @@
#include "../ut/utstring.h"
#include "http.h"
/*
* METHOD_GET, METHOD_POST, METHOD_HEAD, METHOD_PUT,
METHOD_DELETE, METHOD_OPTIONS, METHOD_TRACE,
METHOD_CONNECT, METHOD_OTHER
*/
void http_header_icd_init_f(void* elem) {
memset(elem, 1, sizeof(http_header));
}
void http_header_icd_dtor_f(void* elem) {
http_header *header = (http_header*)elem;
if (header->name!=NULL) free(header->name);
if (header->content!=NULL) free(header->content);
}
UT_icd http_header_icd = {sizeof(http_header), http_header_icd_init_f, NULL, NULL};
UT_icd http_header_icd = {sizeof(http_header), http_header_icd_init_f, NULL, http_header_icd_dtor_f};
char* http_method_getstring(http_request_method method, char* method_other) {
switch(method) {
@@ -207,17 +206,11 @@ void http_header_list_remove(http_header_list *list, const char* name) {
headers = http_header_list_getall(list, name, &count);
for(int i=0; i<count; i++) {
int pos = utarray_eltidx(list,headers[i]);
free(headers[i]->name);
free(headers[i]->content);
utarray_erase(list, pos, 1);
}
free(headers);
}
void http_header_list_delete(http_header_list *list) {
HTTP_HEADER_FOREACH(list, elem) {
free(elem->name);
free(elem->content);
}
utarray_free(list);
}
@@ -242,6 +235,30 @@ void http_request_append_body(http_request *req, const char* body) {
}
strcat(req->body, body);
}
char* http_request_write(http_request *req) {
UT_string *output = calloc(1, sizeof(UT_string));
utstring_init(output);
utstring_printf(output, "%s %s %s\r\n",
http_method_getstring(req->req->method, req->req->method_other),
req->req->uri,
req->req->version == HTTP10 ? "HTTP/1.0" : "HTTP/1.1"
);
HTTP_HEADER_FOREACH(req->headers, elem) {
utstring_printf(output, "%s: %s\r\n",
elem->name, elem->content);
}
utstring_printf(output, "\r\n");
if (req->body != NULL) {
utstring_printf(output, "%s\r\n", req->body);
}
char* result = utstring_body(output);
free(output);
return result;
}
void http_request_delete(http_request *req) {
if (req->req != NULL) {
http_request_line_delete(req->req);
@@ -255,11 +272,12 @@ http_response* http_response_new(http_response_line *resp) {
http_response *response = calloc(1, sizeof(http_response));
response->resp = resp;
response->headers = http_header_list_new();
response->body_chunked = false;
response->body = NULL;
return response;
}
void http_response_append_body(http_response *resp, const char* body) {
uint32_t bodylen = 0;
size_t bodylen = 0;
if (resp->body != NULL) {
bodylen = strlen(resp->body);
}
@@ -290,14 +308,24 @@ char* http_response_write(http_response *resp) {
utstring_printf(output, "%hu %s\r\n", resp->resp->code, http_response_line_get_message(resp->resp));
if (resp->resp->code != 100) { //No additional headers for Continue messages
//Add content length header
uint32_t messageLength = 0;
if (resp->body != NULL) {
messageLength = strlen(resp->body);
if (resp->body_chunked == false) {
//Add content length header
uint32_t messageLength = 0;
if (resp->body != NULL) {
messageLength = strlen(resp->body);
}
char messageLengthStr[100];
snprintf(messageLengthStr, 99, "%u", messageLength);
http_header_list_add(resp->headers, http_header_new(HEADER_CONTENT_LENGTH, messageLengthStr), true);
} else { //Chunked encoding
http_header_list_add(resp->headers, http_header_new(HEADER_TRANSFER_ENCODING, "chunked"), true);
}
//Add content type if not defined
http_header* contenttype = http_header_list_get(resp->headers, HEADER_CONTENT_TYPE);
if (contenttype == NULL) {
http_header_list_add(resp->headers, http_header_new(HEADER_CONTENT_TYPE, DEFAULT_CONTENT_TYPE), false);
}
char messageLengthStr[100];
snprintf(messageLengthStr, 99, "%u", messageLength);
http_header_list_add(resp->headers, http_header_new(HEADER_CONTENT_LENGTH, messageLengthStr), true);
//Add date header
time_t timenow = time(NULL);
@@ -314,9 +342,13 @@ char* http_response_write(http_response *resp) {
utstring_printf(output, "\r\n");
//Write the request
//TODO: chunked support for output
if (resp->body != NULL) {
utstring_printf(output, "%s", resp->body);
if (resp->body_chunked == false && resp->body != NULL) {
utstring_printf(output, "%s\r\n", resp->body);
}
if (resp->body_chunked == true && resp->body != NULL) {
char *chunks = http_chunks_write(resp->body);
utstring_printf(output, "%s", chunks);
free(chunks);
}
char* outputStr = utstring_body(output);
free(output);
@@ -328,10 +360,10 @@ http_response* http_response_create_builtin(uint16_t code, char* errmsg) {
http_header_list_add(resp->headers, http_header_new(HEADER_CONTENT_TYPE, "text/html"), false);
file_map* errorpage = map_file("content/error.html");
file_map* errorpage = file_map_new("content/error.html");
if (errorpage != NULL) {
http_response_append_body(resp, errorpage->map);
free_mapped_file(errorpage);
file_map_delete(errorpage);
} else {
http_response_append_body(resp, "{{title}}\n\n{{message}}");
http_header_list_add(resp->headers, http_header_new(HEADER_CONTENT_TYPE, "text/plain"), true);
@@ -348,3 +380,52 @@ http_response* http_response_create_builtin(uint16_t code, char* errmsg) {
return resp;
}
char* http_chunks_write(char* source) {
size_t sourcelen = strlen(source);
UT_string *output = calloc(1, sizeof(UT_string));
utstring_init(output);
char buffer[HTTP_CHUNK_MAXSIZE+1] = {0};
//determine max chars for length line
sprintf(buffer, "%zx;\r\n", (size_t)HTTP_CHUNK_MAXSIZE);
size_t overhead = strlen(buffer);
overhead+=3;//account for terminating CRLF + \0
buffer[0] = '\0';
size_t i = 0;
while (i < sourcelen) {
//how much can we write in this chunk?
size_t sourcerem = sourcelen - i;
size_t chunklen =
sourcerem > HTTP_CHUNK_MAXSIZE-overhead
? HTTP_CHUNK_MAXSIZE-overhead
: sourcerem;
utstring_printf(output, "%zx;\r\n", chunklen);
memset(&buffer, 0, sizeof(buffer));
strncpy(buffer, source+i, chunklen);
utstring_printf(output, "%s\r\n", buffer);
i += chunklen;
}
char* outputstr = utstring_body(output);
free(output);
return outputstr;
}
char* http_chunks_terminate(http_header_list *footers) {
UT_string *output = calloc(1, sizeof(UT_string));
utstring_init(output);
utstring_printf(output, "0\r\n");
if (footers != NULL) {
//write footers
HTTP_HEADER_FOREACH(footers, elem) {
utstring_printf(output, "%s: %s\r\n", elem->name, elem->content);
}
}
utstring_printf(output, "\r\n");
char* outputstr = utstring_body(output);
free(output);
return outputstr;
}

View File

@@ -30,6 +30,9 @@ extern "C" {
#define HEADER_IF_UNMODIFIED_SINCE "If-Unmodified-Since"
#define FORMAT_HEADER_DATE "%a, %e %h %Y %T %Z"
#define DEFAULT_CONTENT_TYPE "text/plain"
#define HTTP_CHUNK_MAXSIZE 1024*16
typedef enum http_request_method {
METHOD_GET, METHOD_POST, METHOD_HEAD, METHOD_PUT,
@@ -82,6 +85,7 @@ extern "C" {
typedef struct http_response {
http_response_line *resp;
http_header_list *headers;
bool body_chunked;
char* body;
} http_response;
@@ -109,13 +113,18 @@ extern "C" {
http_request* http_request_new();
void http_request_append_body(http_request *req, const char* body);
char* http_request_write(http_request *req);
void http_request_delete(http_request *req);
http_response* http_response_new(http_response_line *resp);
void http_response_append_body(http_response *resp, const char* body);
void http_response_delete(http_response *resp);
char* http_response_write(http_response *resp);
http_response* http_response_create_builtin(uint16_t code, char* errmsg);
char* http_chunks_write(char* source);
char* http_chunks_terminate(http_header_list *footers);
#ifdef __cplusplus
}
#endif

View File

@@ -8,19 +8,14 @@
#include "http.h"
#include "parse.h"
#define GETSTR(str, at, length) do { \
#define GET_CB_STR(str, at, length) do { \
str = calloc(length+1, sizeof(char));\
strncpy(str, at, length);\
}while(0);
#define SKT(parser) ((skt_elem*)parser->data)
http_parser_settings *parser_settings = NULL;
skt_elem *current_socket=NULL;
void parser_set_currentskt(skt_elem *elem) {
current_socket = elem;
}
http_parser_settings* parser_get_settings(skt_elem *elem) {
if (parser_settings == NULL) {
parser_settings = calloc(1, sizeof(http_parser_settings));
@@ -30,111 +25,103 @@ http_parser_settings* parser_get_settings(skt_elem *elem) {
parser_settings->on_headers_complete = parser_cb_on_headers_complete;
parser_settings->on_message_begin = parser_cb_on_message_begin;
parser_settings->on_message_complete = parser_cb_on_message_complete;
parser_settings->on_status_complete = parser_cb_on_status;
parser_settings->on_status = parser_cb_on_status;
parser_settings->on_url = parser_cb_on_url;
}
parser_set_currentskt(elem);
return parser_settings;
}
int parser_cb_on_message_begin(http_parser* parser) {
info("parser_cb_on_message_begin");
if (current_socket->current_request != NULL) {
http_request_delete(current_socket->current_request);
if (SKT(parser)->current_request != NULL) {
http_request_delete(SKT(parser)->current_request);
}
current_socket->current_request = http_request_new();
SKT(parser)->current_request = http_request_new();
return 0;
}
int parser_cb_on_url(http_parser* parser, const char *at, size_t length) {
char* str;GETSTR(str,at,length);
info("parser_cb_on_url: %s",str);
current_socket->current_request->req = http_request_line_new(http_method_fromstring(http_method_str(parser->method)), NULL);
current_socket->current_request->req->uri = str;
char* str;GET_CB_STR(str,at,length);
SKT(parser)->current_request->req = http_request_line_new(http_method_fromstring(http_method_str(parser->method)), NULL);
SKT(parser)->current_request->req->uri = str;
return 0;
}
int parser_cb_on_status(http_parser* parser) {
int parser_cb_on_status(http_parser* parser, const char *at, size_t length) {
//Responses only, so ignored
info("parser_cb_on_status");
return 0;
}
int parser_cb_on_header_field(http_parser* parser, const char *at, size_t length) {
char* str;GETSTR(str,at,length);
info("parser_cb_on_header_field: %s",str);
char* str;GET_CB_STR(str,at,length);
if (current_socket->parser_header_state == HSTATE_NONE) {
if (SKT(parser)->parser_header_state == HSTATE_NONE) {
//First call, new header
if (current_socket->parser_current_header != NULL) {
http_header_delete(current_socket->parser_current_header);
if (SKT(parser)->parser_current_header != NULL) {
http_header_delete(SKT(parser)->parser_current_header);
}
current_socket->parser_current_header = http_header_new(str, NULL);
} else if (current_socket->parser_header_state == HSTATE_VALUE) {
SKT(parser)->parser_current_header = http_header_new(str, NULL);
//the http version should also be set now
if (parser->http_major == 1) {
if (parser->http_minor == 0) {
SKT(parser)->current_request->req->version = HTTP10;
} else if (parser->http_minor == 1) {
SKT(parser)->current_request->req->version = HTTP11;
} else {
return -1;
}
} else {
return -1;
}
} else if (SKT(parser)->parser_header_state == HSTATE_VALUE) {
//New header
if (current_socket->parser_current_header != NULL) {
http_header_list_add(current_socket->current_request->headers, current_socket->parser_current_header, false);
if (SKT(parser)->parser_current_header != NULL) {
http_header_list_add(SKT(parser)->current_request->headers, SKT(parser)->parser_current_header, false);
}
current_socket->parser_current_header = http_header_new(str, NULL);
} else if (current_socket->parser_header_state == HSTATE_FIELD) {
SKT(parser)->parser_current_header = http_header_new(str, NULL);
} else if (SKT(parser)->parser_header_state == HSTATE_FIELD) {
//continuation of current headers name
http_header* header = current_socket->parser_current_header;
http_header* header = SKT(parser)->parser_current_header;
size_t newlen = strlen(header->name) + length +1;
header->name = realloc(header->name, newlen * sizeof(char));
} else {
return 1;
}
current_socket->parser_header_state = HSTATE_FIELD;
SKT(parser)->parser_header_state = HSTATE_FIELD;
free(str);
return 0;
}
int parser_cb_on_header_value(http_parser* parser, const char *at, size_t length) {
char* str;GETSTR(str,at,length);
info("parser_cb_on_header_value: %s",str);
char* str;GET_CB_STR(str,at,length);
http_header_append_content(current_socket->parser_current_header, str);
http_header_append_content(SKT(parser)->parser_current_header, str);
current_socket->parser_header_state = HSTATE_VALUE;
SKT(parser)->parser_header_state = HSTATE_VALUE;
free(str);
return 0;
}
int parser_cb_on_headers_complete(http_parser* parser) {
info("parser_cb_on_headers_complete");
//save current header
if (current_socket->parser_current_header != NULL) {
http_header_list_add(current_socket->current_request->headers, current_socket->parser_current_header, false);
current_socket->parser_current_header = NULL;
}
//the http version should also be set now
if (parser->http_major == 1) {
if (parser->http_minor == 0) {
current_socket->current_request->req->version = HTTP10;
} else if (parser->http_minor == 1) {
current_socket->current_request->req->version = HTTP11;
} else {
return -1;
}
} else {
return -1;
if (SKT(parser)->parser_current_header != NULL) {
http_header_list_add(SKT(parser)->current_request->headers, SKT(parser)->parser_current_header, false);
SKT(parser)->parser_current_header = NULL;
}
return 0;
}
int parser_cb_on_body(http_parser* parser, const char *at, size_t length) {
char* str;GETSTR(str,at,length);
info("parser_cb_on_body: %s",str);
char* str;GET_CB_STR(str,at,length);
http_request_append_body(current_socket->current_request, str);
http_request_append_body(SKT(parser)->current_request, str);
free(str);
return 0;
}
int parser_cb_on_message_complete(http_parser* parser) {
info("parser_cb_on_message_complete");
current_socket->request_complete = true;
SKT(parser)->request_complete = true;
return 0;
}

View File

@@ -12,7 +12,7 @@
extern "C" {
#endif
#include <http_parser.h>
#include "http_parser.h"
#include "http.h"
#include "../main.h"
@@ -23,7 +23,7 @@ extern "C" {
int parser_cb_on_message_begin(http_parser* parser);
int parser_cb_on_url(http_parser* parser, const char *at, size_t length);
int parser_cb_on_status(http_parser* parser);
int parser_cb_on_status(http_parser* parser, const char *at, size_t length);
int parser_cb_on_header_field(http_parser* parser, const char *at, size_t length);
int parser_cb_on_header_value(http_parser* parser, const char *at, size_t length);
int parser_cb_on_headers_complete(http_parser* parser);

View File

@@ -14,7 +14,8 @@
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <http_parser.h>
#include "http_parser.h"
#include "ut/utlist.h"
#include "ut/utarray.h"
@@ -60,11 +61,20 @@ int main(int argc, char** argv) {
utstring_body(elem->info->read),
utstring_len(elem->info->read));
if (parsedcount != utstring_len(elem->info->read)) {
warning("error parsing request. closing connection", false);
char warningmsg[2048] = {0};
snprintf(warningmsg, 2048,
"error parsing request (%s: %s). closing connection",
http_errno_name(elem->parser->http_errno),
http_errno_description(elem->parser->http_errno));
warning(warningmsg, false);
elem->info->close = true;
}
utstring_clear(elem->info->read);
if (elem->request_complete == true) {
char* reqstr = http_request_write(elem->current_request);
info("\n%s\n", reqstr);
free(reqstr);
http_response* resp = http_response_create_builtin(200, elem->current_request->req->uri);
utstring_printf(elem->info->write, "%s", http_response_write(resp));
http_response_delete(resp);
@@ -123,6 +133,7 @@ skt_elem* skt_elem_new(skt_info *info) {
elem->info = info;
elem->parser = calloc(1, sizeof(http_parser));
http_parser_init(elem->parser, HTTP_REQUEST);
elem->parser->data = (void*)elem;
elem->parser_header_state = HSTATE_NONE;
elem->request_complete = false;
return elem;
@@ -139,10 +150,9 @@ void fatal(char* msg) {
exit(EXIT_FAILURE);
}
void warning(char* msg, bool showPError) {
char warning[512];
bzero(&warning, sizeof warning);
snprintf(warning, 511, "Warning: %s", msg);
char warning[1024];
memset(&warning, 0, 1024*sizeof(char));
snprintf(warning, 1024, "Warning: %s", msg);
if (showPError == true) {
perror(warning);
@@ -200,7 +210,7 @@ char** str_splitlines(char *str, size_t *line_count) {
return result;
}
file_map* map_file(const char* filename) {
file_map* file_map_new(const char* filename) {
int fd = open(filename, O_RDONLY);
if (fd < 0) {
@@ -221,7 +231,7 @@ file_map* map_file(const char* filename) {
filemap->size = size;
return filemap;
}
void free_mapped_file(file_map* file) {
void file_map_delete(file_map* file) {
if (munmap((void*)file->map, file->size) < 0) {
warning("failed to unmap file", true);
}

View File

@@ -13,7 +13,7 @@ extern "C" {
#endif
#include <stdbool.h>
#include <http_parser.h>
#include "http_parser.h"
#include "socket.h"
#include "http/http.h"
@@ -45,8 +45,8 @@ extern "C" {
char** str_splitlines(char *str, size_t *line_count);
char* str_replace(char *str, const char *search, const char *replacement);
file_map* map_file(const char* filename);
void free_mapped_file(file_map* map);
file_map* file_map_new(const char* filename);
void file_map_delete(file_map* map);
#ifdef __cplusplus