| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 045e04e commit 7caeb14
7 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -65,3 +65,4 @@ Romain Giraud <giraud.romain@gmail.com> | |||
| 65 | 65 | Jay Satiro <raysatiro@yahoo.com> | |
| 66 | 66 | Arne Steen <Arne.Steen@gmx.de> | |
| 67 | 67 | Kjell Schubert <kjell.schubert@gmail.com> | |
| 68 | + Olivier Mengué <dolmen@cpan.org> | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,7 +19,19 @@ | |||
| 19 | 19 | # IN THE SOFTWARE. | |
| 20 | 20 | ||
| 21 | 21 | PLATFORM ?= $(shell sh -c 'uname -s | tr "[A-Z]" "[a-z]"') | |
| 22 | - SONAME ?= libhttp_parser.so.2.5.0 | ||
| 22 | + HELPER ?= | ||
| 23 | + BINEXT ?= | ||
| 24 | + ifeq (darwin,$(PLATFORM)) | ||
| 25 | + SONAME ?= libhttp_parser.2.6.0.dylib | ||
| 26 | + SOEXT ?= dylib | ||
| 27 | + else ifeq (wine,$(PLATFORM)) | ||
| 28 | + CC = winegcc | ||
| 29 | + BINEXT = .exe.so | ||
| 30 | + HELPER = wine | ||
| 31 | + else | ||
| 32 | + SONAME ?= libhttp_parser.so.2.6.0 | ||
| 33 | + SOEXT ?= so | ||
| 34 | + endif | ||
| 23 | 35 | ||
| 24 | 36 | CC?=gcc | |
| 25 | 37 | AR?=ar | |
@@ -53,8 +65,8 @@ LDFLAGS_LIB += -Wl,-soname=$(SONAME) | |||
| 53 | 65 | endif | |
| 54 | 66 | ||
| 55 | 67 | test: test_g test_fast | |
| 56 | - ./test_g | ||
| 57 | - ./test_fast | ||
| 68 | + $(HELPER) ./test_g$(BINEXT) | ||
| 69 | + $(HELPER) ./test_fast$(BINEXT) | ||
| 58 | 70 | ||
| 59 | 71 | test_g: http_parser_g.o test_g.o | |
| 60 | 72 | $(CC) $(CFLAGS_DEBUG) $(LDFLAGS) http_parser_g.o test_g.o -o $@ | |
@@ -81,7 +93,7 @@ http_parser.o: http_parser.c http_parser.h Makefile | |||
| 81 | 93 | $(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) -c http_parser.c | |
| 82 | 94 | ||
| 83 | 95 | test-run-timed: test_fast | |
| 84 | - while(true) do time ./test_fast > /dev/null; done | ||
| 96 | + while(true) do time $(HELPER) ./test_fast$(BINEXT) > /dev/null; done | ||
| 85 | 97 | ||
| 86 | 98 | test-valgrind: test_g | |
| 87 | 99 | valgrind ./test_g | |
@@ -102,23 +114,23 @@ url_parser_g: http_parser_g.o contrib/url_parser.c | |||
| 102 | 114 | $(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) $^ -o $@ | |
| 103 | 115 | ||
| 104 | 116 | parsertrace: http_parser.o contrib/parsertrace.c | |
| 105 | - $(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) $^ -o parsertrace | ||
| 117 | + $(CC) $(CPPFLAGS_FAST) $(CFLAGS_FAST) $^ -o parsertrace$(BINEXT) | ||
| 106 | 118 | ||
| 107 | 119 | parsertrace_g: http_parser_g.o contrib/parsertrace.c | |
| 108 | - $(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) $^ -o parsertrace_g | ||
| 120 | + $(CC) $(CPPFLAGS_DEBUG) $(CFLAGS_DEBUG) $^ -o parsertrace_g$(BINEXT) | ||
| 109 | 121 | ||
| 110 | 122 | tags: http_parser.c http_parser.h test.c | |
| 111 | 123 | ctags $^ | |
| 112 | 124 | ||
| 113 | 125 | install: library | |
| 114 | 126 | $(INSTALL) -D http_parser.h $(INCLUDEDIR)/http_parser.h | |
| 115 | 127 | $(INSTALL) -D $(SONAME) $(LIBDIR)/$(SONAME) | |
| 116 | - ln -s $(LIBDIR)/$(SONAME) $(LIBDIR)/libhttp_parser.so | ||
| 128 | + ln -s $(LIBDIR)/$(SONAME) $(LIBDIR)/libhttp_parser.$(SOEXT) | ||
| 117 | 129 | ||
| 118 | 130 | install-strip: library | |
| 119 | 131 | $(INSTALL) -D http_parser.h $(INCLUDEDIR)/http_parser.h | |
| 120 | 132 | $(INSTALL) -D -s $(SONAME) $(LIBDIR)/$(SONAME) | |
| 121 | - ln -s $(LIBDIR)/$(SONAME) $(LIBDIR)/libhttp_parser.so | ||
| 133 | + ln -s $(LIBDIR)/$(SONAME) $(LIBDIR)/libhttp_parser.$(SOEXT) | ||
| 122 | 134 | ||
| 123 | 135 | uninstall: | |
| 124 | 136 | rm $(INCLUDEDIR)/http_parser.h | |
@@ -128,7 +140,8 @@ uninstall: | |||
| 128 | 140 | clean: | |
| 129 | 141 | rm -f *.o *.a tags test test_fast test_g \ | |
| 130 | 142 | http_parser.tar libhttp_parser.so.* \ | |
| 131 | - url_parser url_parser_g parsertrace parsertrace_g | ||
| 143 | + url_parser url_parser_g parsertrace parsertrace_g \ | ||
| 144 | + *.exe *.exe.so | ||
| 132 | 145 | ||
| 133 | 146 | contrib/url_parser.c: http_parser.h | |
| 134 | 147 | contrib/parsertrace.c: http_parser.h | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -94,7 +94,7 @@ The Special Problem of Upgrade | |||
| 94 | 94 | ------------------------------ | |
| 95 | 95 | ||
| 96 | 96 | HTTP supports upgrading the connection to a different protocol. An | |
| 97 | - increasingly common example of this is the Web Socket protocol which sends | ||
| 97 | + increasingly common example of this is the WebSocket protocol which sends | ||
| 98 | 98 | a request like | |
| 99 | 99 | ||
| 100 | 100 | GET /demo HTTP/1.1 | |
@@ -106,8 +106,8 @@ a request like | |||
| 106 | 106 | ||
| 107 | 107 | followed by non-HTTP data. | |
| 108 | 108 | ||
| 109 | - (See http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75 for more | ||
| 110 | - information the Web Socket protocol.) | ||
| 109 | + (See [RFC6455](https://tools.ietf.org/html/rfc6455) for more information the | ||
| 110 | + WebSocket protocol.) | ||
| 111 | 111 | ||
| 112 | 112 | To support this, the parser will treat this as a normal HTTP message without a | |
| 113 | 113 | body, issuing both on_headers_complete and on_message_complete callbacks. However | |
@@ -137,6 +137,69 @@ There are two types of callbacks: | |||
| 137 | 137 | Callbacks must return 0 on success. Returning a non-zero value indicates | |
| 138 | 138 | error to the parser, making it exit immediately. | |
| 139 | 139 | ||
| 140 | + For cases where it is necessary to pass local information to/from a callback, | ||
| 141 | + the `http_parser` object's `data` field can be used. | ||
| 142 | + An example of such a case is when using threads to handle a socket connection, | ||
| 143 | + parse a request, and then give a response over that socket. By instantiation | ||
| 144 | + of a thread-local struct containing relevant data (e.g. accepted socket, | ||
| 145 | + allocated memory for callbacks to write into, etc), a parser's callbacks are | ||
| 146 | + able to communicate data between the scope of the thread and the scope of the | ||
| 147 | + callback in a threadsafe manner. This allows http-parser to be used in | ||
| 148 | + multi-threaded contexts. | ||
| 149 | + | ||
| 150 | + Example: | ||
| 151 | + ``` | ||
| 152 | + typedef struct { | ||
| 153 | + socket_t sock; | ||
| 154 | + void* buffer; | ||
| 155 | + int buf_len; | ||
| 156 | + } custom_data_t; | ||
| 157 | + | ||
| 158 | + | ||
| 159 | + int my_url_callback(http_parser* parser, const char *at, size_t length) { | ||
| 160 | + /* access to thread local custom_data_t struct. | ||
| 161 | + Use this access save parsed data for later use into thread local | ||
| 162 | + buffer, or communicate over socket | ||
| 163 | + */ | ||
| 164 | + parser->data; | ||
| 165 | + ... | ||
| 166 | + return 0; | ||
| 167 | + } | ||
| 168 | + | ||
| 169 | + ... | ||
| 170 | + | ||
| 171 | + void http_parser_thread(socket_t sock) { | ||
| 172 | + int nparsed = 0; | ||
| 173 | + /* allocate memory for user data */ | ||
| 174 | + custom_data_t *my_data = malloc(sizeof(custom_data_t)); | ||
| 175 | + | ||
| 176 | + /* some information for use by callbacks. | ||
| 177 | + * achieves thread -> callback information flow */ | ||
| 178 | + my_data->sock = sock; | ||
| 179 | + | ||
| 180 | + /* instantiate a thread-local parser */ | ||
| 181 | + http_parser *parser = malloc(sizeof(http_parser)); | ||
| 182 | + http_parser_init(parser, HTTP_REQUEST); /* initialise parser */ | ||
| 183 | + /* this custom data reference is accessible through the reference to the | ||
| 184 | + parser supplied to callback functions */ | ||
| 185 | + parser->data = my_data; | ||
| 186 | + | ||
| 187 | + http_parser_settings settings; / * set up callbacks */ | ||
| 188 | + settings.on_url = my_url_callback; | ||
| 189 | + | ||
| 190 | + /* execute parser */ | ||
| 191 | + nparsed = http_parser_execute(parser, &settings, buf, recved); | ||
| 192 | + | ||
| 193 | + ... | ||
| 194 | + /* parsed information copied from callback. | ||
| 195 | + can now perform action on data copied into thread-local memory from callbacks. | ||
| 196 | + achieves callback -> thread information flow */ | ||
| 197 | + my_data->buffer; | ||
| 198 | + ... | ||
| 199 | + } | ||
| 200 | + | ||
| 201 | + ``` | ||
| 202 | + | ||
| 140 | 203 | In case you parse HTTP message in chunks (i.e. `read()` request line | |
| 141 | 204 | from socket, parse, read half headers, parse, etc) your data callbacks | |
| 142 | 205 | may be called more than once. Http-parser guarantees that data pointer is only | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,6 +35,7 @@ int main(int argc, char ** argv) { | |||
| 35 | 35 | connect = strcmp("connect", argv[1]) == 0 ? 1 : 0; | |
| 36 | 36 | printf("Parsing %s, connect %d\n", argv[2], connect); | |
| 37 | 37 | ||
| 38 | + http_parser_url_init(&u); | ||
| 38 | 39 | result = http_parser_parse_url(argv[2], len, connect, &u); | |
| 39 | 40 | if (result != 0) { | |
| 40 | 41 | printf("Parse error : %d\n", result); | |
@@ -43,4 +44,4 @@ int main(int argc, char ** argv) { | |||
| 43 | 44 | printf("Parse ok, result : \n"); | |
| 44 | 45 | dump_url(argv[2], &u); | |
| 45 | 46 | return 0; | |
| 46 | - } | ||
| 47 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments