mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-02-01 08:48:01 +00:00
Revert removal of Http. Witness my indecisiveness.
This commit is contained in:
parent
fb975ead23
commit
dd203f0065
@ -16,6 +16,12 @@ ZeroTier One includes or links with the following third party software:
|
|||||||
* LZ4 compression algorithm by Yann Collet (BSD license)
|
* LZ4 compression algorithm by Yann Collet (BSD license)
|
||||||
http://code.google.com/p/lz4/
|
http://code.google.com/p/lz4/
|
||||||
|
|
||||||
|
* JsonCPP by Baptiste Lepilleur (public domain)
|
||||||
|
http://jsoncpp.sourceforge.net
|
||||||
|
|
||||||
|
* http-parser, a simple C http parser library (MIT license)
|
||||||
|
https://github.com/joyent/http-parser
|
||||||
|
|
||||||
* OpenSSL libcrypto (BSD-style OpenSSL license)
|
* OpenSSL libcrypto (BSD-style OpenSSL license)
|
||||||
http://www.openssl.org/
|
http://www.openssl.org/
|
||||||
|
|
||||||
|
41
ext/http-parser/AUTHORS
Normal file
41
ext/http-parser/AUTHORS
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
# Authors ordered by first contribution.
|
||||||
|
Ryan Dahl <ry@tinyclouds.org>
|
||||||
|
Jeremy Hinegardner <jeremy@hinegardner.org>
|
||||||
|
Sergey Shepelev <temotor@gmail.com>
|
||||||
|
Joe Damato <ice799@gmail.com>
|
||||||
|
tomika <tomika_nospam@freemail.hu>
|
||||||
|
Phoenix Sol <phoenix@burninglabs.com>
|
||||||
|
Cliff Frey <cliff@meraki.com>
|
||||||
|
Ewen Cheslack-Postava <ewencp@cs.stanford.edu>
|
||||||
|
Santiago Gala <sgala@apache.org>
|
||||||
|
Tim Becker <tim.becker@syngenio.de>
|
||||||
|
Jeff Terrace <jterrace@gmail.com>
|
||||||
|
Ben Noordhuis <info@bnoordhuis.nl>
|
||||||
|
Nathan Rajlich <nathan@tootallnate.net>
|
||||||
|
Mark Nottingham <mnot@mnot.net>
|
||||||
|
Aman Gupta <aman@tmm1.net>
|
||||||
|
Tim Becker <tim.becker@kuriositaet.de>
|
||||||
|
Sean Cunningham <sean.cunningham@mandiant.com>
|
||||||
|
Peter Griess <pg@std.in>
|
||||||
|
Salman Haq <salman.haq@asti-usa.com>
|
||||||
|
Cliff Frey <clifffrey@gmail.com>
|
||||||
|
Jon Kolb <jon@b0g.us>
|
||||||
|
Fouad Mardini <f.mardini@gmail.com>
|
||||||
|
Paul Querna <pquerna@apache.org>
|
||||||
|
Felix Geisendörfer <felix@debuggable.com>
|
||||||
|
koichik <koichik@improvement.jp>
|
||||||
|
Andre Caron <andre.l.caron@gmail.com>
|
||||||
|
Ivo Raisr <ivosh@ivosh.net>
|
||||||
|
James McLaughlin <jamie@lacewing-project.org>
|
||||||
|
David Gwynne <loki@animata.net>
|
||||||
|
Thomas LE ROUX <thomas@november-eleven.fr>
|
||||||
|
Randy Rizun <rrizun@ortivawireless.com>
|
||||||
|
Andre Louis Caron <andre.louis.caron@usherbrooke.ca>
|
||||||
|
Simon Zimmermann <simonz05@gmail.com>
|
||||||
|
Erik Dubbelboer <erik@dubbelboer.com>
|
||||||
|
Martell Malone <martellmalone@gmail.com>
|
||||||
|
Bertrand Paquet <bpaquet@octo.com>
|
||||||
|
BogDan Vatra <bogdan@kde.org>
|
||||||
|
Peter Faiman <peter@thepicard.org>
|
||||||
|
Corey Richardson <corey@octayn.net>
|
||||||
|
Tóth Tamás <tomika_nospam@freemail.hu>
|
4
ext/http-parser/CONTRIBUTIONS
Normal file
4
ext/http-parser/CONTRIBUTIONS
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
Contributors must agree to the Contributor License Agreement before patches
|
||||||
|
can be accepted.
|
||||||
|
|
||||||
|
http://spreadsheets2.google.com/viewform?hl=en&formkey=dDJXOGUwbzlYaWM4cHN1MERwQS1CSnc6MQ
|
23
ext/http-parser/LICENSE-MIT
Normal file
23
ext/http-parser/LICENSE-MIT
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
http_parser.c is based on src/http/ngx_http_parse.c from NGINX copyright
|
||||||
|
Igor Sysoev.
|
||||||
|
|
||||||
|
Additional changes are licensed under the same terms as NGINX and
|
||||||
|
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.
|
180
ext/http-parser/README.md
Normal file
180
ext/http-parser/README.md
Normal file
@ -0,0 +1,180 @@
|
|||||||
|
HTTP Parser
|
||||||
|
===========
|
||||||
|
|
||||||
|
[![Build Status](https://travis-ci.org/joyent/http-parser.png?branch=master)](https://travis-ci.org/joyent/http-parser)
|
||||||
|
|
||||||
|
This is a parser for HTTP messages written in C. It parses both requests and
|
||||||
|
responses. The parser is designed to be used in performance HTTP
|
||||||
|
applications. It does not make any syscalls nor allocations, it does not
|
||||||
|
buffer data, it can be interrupted at anytime. Depending on your
|
||||||
|
architecture, it only requires about 40 bytes of data per message
|
||||||
|
stream (in a web server that is per connection).
|
||||||
|
|
||||||
|
Features:
|
||||||
|
|
||||||
|
* No dependencies
|
||||||
|
* Handles persistent streams (keep-alive).
|
||||||
|
* Decodes chunked encoding.
|
||||||
|
* Upgrade support
|
||||||
|
* Defends against buffer overflow attacks.
|
||||||
|
|
||||||
|
The parser extracts the following information from HTTP messages:
|
||||||
|
|
||||||
|
* Header fields and values
|
||||||
|
* Content-Length
|
||||||
|
* Request method
|
||||||
|
* Response status code
|
||||||
|
* Transfer-Encoding
|
||||||
|
* HTTP version
|
||||||
|
* Request URL
|
||||||
|
* Message body
|
||||||
|
|
||||||
|
|
||||||
|
Usage
|
||||||
|
-----
|
||||||
|
|
||||||
|
One `http_parser` object is used per TCP connection. Initialize the struct
|
||||||
|
using `http_parser_init()` and set the callbacks. That might look something
|
||||||
|
like this for a request parser:
|
||||||
|
|
||||||
|
http_parser_settings settings;
|
||||||
|
settings.on_url = my_url_callback;
|
||||||
|
settings.on_header_field = my_header_field_callback;
|
||||||
|
/* ... */
|
||||||
|
|
||||||
|
http_parser *parser = malloc(sizeof(http_parser));
|
||||||
|
http_parser_init(parser, HTTP_REQUEST);
|
||||||
|
parser->data = my_socket;
|
||||||
|
|
||||||
|
When data is received on the socket execute the parser and check for errors.
|
||||||
|
|
||||||
|
size_t len = 80*1024, nparsed;
|
||||||
|
char buf[len];
|
||||||
|
ssize_t recved;
|
||||||
|
|
||||||
|
recved = recv(fd, buf, len, 0);
|
||||||
|
|
||||||
|
if (recved < 0) {
|
||||||
|
/* Handle error. */
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Start up / continue the parser.
|
||||||
|
* Note we pass recved==0 to signal that EOF has been recieved.
|
||||||
|
*/
|
||||||
|
nparsed = http_parser_execute(parser, &settings, buf, recved);
|
||||||
|
|
||||||
|
if (parser->upgrade) {
|
||||||
|
/* handle new protocol */
|
||||||
|
} else if (nparsed != recved) {
|
||||||
|
/* Handle error. Usually just close the connection. */
|
||||||
|
}
|
||||||
|
|
||||||
|
HTTP needs to know where the end of the stream is. For example, sometimes
|
||||||
|
servers send responses without Content-Length and expect the client to
|
||||||
|
consume input (for the body) until EOF. To tell http_parser about EOF, give
|
||||||
|
`0` as the forth parameter to `http_parser_execute()`. Callbacks and errors
|
||||||
|
can still be encountered during an EOF, so one must still be prepared
|
||||||
|
to receive them.
|
||||||
|
|
||||||
|
Scalar valued message information such as `status_code`, `method`, and the
|
||||||
|
HTTP version are stored in the parser structure. This data is only
|
||||||
|
temporally stored in `http_parser` and gets reset on each new message. If
|
||||||
|
this information is needed later, copy it out of the structure during the
|
||||||
|
`headers_complete` callback.
|
||||||
|
|
||||||
|
The parser decodes the transfer-encoding for both requests and responses
|
||||||
|
transparently. That is, a chunked encoding is decoded before being sent to
|
||||||
|
the on_body callback.
|
||||||
|
|
||||||
|
|
||||||
|
The Special Problem of Upgrade
|
||||||
|
------------------------------
|
||||||
|
|
||||||
|
HTTP supports upgrading the connection to a different protocol. An
|
||||||
|
increasingly common example of this is the Web Socket protocol which sends
|
||||||
|
a request like
|
||||||
|
|
||||||
|
GET /demo HTTP/1.1
|
||||||
|
Upgrade: WebSocket
|
||||||
|
Connection: Upgrade
|
||||||
|
Host: example.com
|
||||||
|
Origin: http://example.com
|
||||||
|
WebSocket-Protocol: sample
|
||||||
|
|
||||||
|
followed by non-HTTP data.
|
||||||
|
|
||||||
|
(See http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-75 for more
|
||||||
|
information the Web Socket protocol.)
|
||||||
|
|
||||||
|
To support this, the parser will treat this as a normal HTTP message without a
|
||||||
|
body. Issuing both on_headers_complete and on_message_complete callbacks. However
|
||||||
|
http_parser_execute() will stop parsing at the end of the headers and return.
|
||||||
|
|
||||||
|
The user is expected to check if `parser->upgrade` has been set to 1 after
|
||||||
|
`http_parser_execute()` returns. Non-HTTP data begins at the buffer supplied
|
||||||
|
offset by the return value of `http_parser_execute()`.
|
||||||
|
|
||||||
|
|
||||||
|
Callbacks
|
||||||
|
---------
|
||||||
|
|
||||||
|
During the `http_parser_execute()` call, the callbacks set in
|
||||||
|
`http_parser_settings` will be executed. The parser maintains state and
|
||||||
|
never looks behind, so buffering the data is not necessary. If you need to
|
||||||
|
save certain data for later usage, you can do that from the callbacks.
|
||||||
|
|
||||||
|
There are two types of callbacks:
|
||||||
|
|
||||||
|
* notification `typedef int (*http_cb) (http_parser*);`
|
||||||
|
Callbacks: on_message_begin, on_headers_complete, on_message_complete.
|
||||||
|
* data `typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);`
|
||||||
|
Callbacks: (requests only) on_uri,
|
||||||
|
(common) on_header_field, on_header_value, on_body;
|
||||||
|
|
||||||
|
Callbacks must return 0 on success. Returning a non-zero value indicates
|
||||||
|
error to the parser, making it exit immediately.
|
||||||
|
|
||||||
|
In case you parse HTTP message in chunks (i.e. `read()` request line
|
||||||
|
from socket, parse, read half headers, parse, etc) your data callbacks
|
||||||
|
may be called more than once. Http-parser guarantees that data pointer is only
|
||||||
|
valid for the lifetime of callback. You can also `read()` into a heap allocated
|
||||||
|
buffer to avoid copying memory around if this fits your application.
|
||||||
|
|
||||||
|
Reading headers may be a tricky task if you read/parse headers partially.
|
||||||
|
Basically, you need to remember whether last header callback was field or value
|
||||||
|
and apply following logic:
|
||||||
|
|
||||||
|
(on_header_field and on_header_value shortened to on_h_*)
|
||||||
|
------------------------ ------------ --------------------------------------------
|
||||||
|
| State (prev. callback) | Callback | Description/action |
|
||||||
|
------------------------ ------------ --------------------------------------------
|
||||||
|
| nothing (first call) | on_h_field | Allocate new buffer and copy callback data |
|
||||||
|
| | | into it |
|
||||||
|
------------------------ ------------ --------------------------------------------
|
||||||
|
| value | on_h_field | New header started. |
|
||||||
|
| | | Copy current name,value buffers to headers |
|
||||||
|
| | | list and allocate new buffer for new name |
|
||||||
|
------------------------ ------------ --------------------------------------------
|
||||||
|
| field | on_h_field | Previous name continues. Reallocate name |
|
||||||
|
| | | buffer and append callback data to it |
|
||||||
|
------------------------ ------------ --------------------------------------------
|
||||||
|
| field | on_h_value | Value for current header started. Allocate |
|
||||||
|
| | | new buffer and copy callback data to it |
|
||||||
|
------------------------ ------------ --------------------------------------------
|
||||||
|
| value | on_h_value | Value continues. Reallocate value buffer |
|
||||||
|
| | | and append callback data to it |
|
||||||
|
------------------------ ------------ --------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
Parsing URLs
|
||||||
|
------------
|
||||||
|
|
||||||
|
A simplistic zero-copy URL parser is provided as `http_parser_parse_url()`.
|
||||||
|
Users of this library may wish to use it to parse URLs constructed from
|
||||||
|
consecutive `on_url` callbacks.
|
||||||
|
|
||||||
|
See examples of reading in headers:
|
||||||
|
|
||||||
|
* [partial example](http://gist.github.com/155877) in C
|
||||||
|
* [from http-parser tests](http://github.com/joyent/http-parser/blob/37a0ff8/test.c#L403) in C
|
||||||
|
* [from Node library](http://github.com/joyent/node/blob/842eaf4/src/http.js#L284) in Javascript
|
156
ext/http-parser/contrib/parsertrace.c
Normal file
156
ext/http-parser/contrib/parsertrace.c
Normal file
@ -0,0 +1,156 @@
|
|||||||
|
/* Based on src/http/ngx_http_parse.c from NGINX copyright Igor Sysoev
|
||||||
|
*
|
||||||
|
* Additional changes are licensed under the same terms as NGINX and
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* Dump what the parser finds to stdout as it happen */
|
||||||
|
|
||||||
|
#include "http_parser.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
int on_message_begin(http_parser* _) {
|
||||||
|
(void)_;
|
||||||
|
printf("\n***MESSAGE BEGIN***\n\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int on_headers_complete(http_parser* _) {
|
||||||
|
(void)_;
|
||||||
|
printf("\n***HEADERS COMPLETE***\n\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int on_message_complete(http_parser* _) {
|
||||||
|
(void)_;
|
||||||
|
printf("\n***MESSAGE COMPLETE***\n\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int on_url(http_parser* _, const char* at, size_t length) {
|
||||||
|
(void)_;
|
||||||
|
printf("Url: %.*s\n", (int)length, at);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int on_header_field(http_parser* _, const char* at, size_t length) {
|
||||||
|
(void)_;
|
||||||
|
printf("Header field: %.*s\n", (int)length, at);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int on_header_value(http_parser* _, const char* at, size_t length) {
|
||||||
|
(void)_;
|
||||||
|
printf("Header value: %.*s\n", (int)length, at);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int on_body(http_parser* _, const char* at, size_t length) {
|
||||||
|
(void)_;
|
||||||
|
printf("Body: %.*s\n", (int)length, at);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void usage(const char* name) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"Usage: %s $type $filename\n"
|
||||||
|
" type: -x, where x is one of {r,b,q}\n"
|
||||||
|
" parses file as a Response, reQuest, or Both\n",
|
||||||
|
name);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
enum http_parser_type file_type;
|
||||||
|
|
||||||
|
if (argc != 3) {
|
||||||
|
usage(argv[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
char* type = argv[1];
|
||||||
|
if (type[0] != '-') {
|
||||||
|
usage(argv[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (type[1]) {
|
||||||
|
/* in the case of "-", type[1] will be NUL */
|
||||||
|
case 'r':
|
||||||
|
file_type = HTTP_RESPONSE;
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
file_type = HTTP_REQUEST;
|
||||||
|
break;
|
||||||
|
case 'b':
|
||||||
|
file_type = HTTP_BOTH;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
usage(argv[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
char* filename = argv[2];
|
||||||
|
FILE* file = fopen(filename, "r");
|
||||||
|
if (file == NULL) {
|
||||||
|
perror("fopen");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
fseek(file, 0, SEEK_END);
|
||||||
|
long file_length = ftell(file);
|
||||||
|
if (file_length == -1) {
|
||||||
|
perror("ftell");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
fseek(file, 0, SEEK_SET);
|
||||||
|
|
||||||
|
char* data = malloc(file_length);
|
||||||
|
if (fread(data, 1, file_length, file) != (size_t)file_length) {
|
||||||
|
fprintf(stderr, "couldn't read entire file\n");
|
||||||
|
free(data);
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
http_parser_settings settings;
|
||||||
|
memset(&settings, 0, sizeof(settings));
|
||||||
|
settings.on_message_begin = on_message_begin;
|
||||||
|
settings.on_url = on_url;
|
||||||
|
settings.on_header_field = on_header_field;
|
||||||
|
settings.on_header_value = on_header_value;
|
||||||
|
settings.on_headers_complete = on_headers_complete;
|
||||||
|
settings.on_body = on_body;
|
||||||
|
settings.on_message_complete = on_message_complete;
|
||||||
|
|
||||||
|
http_parser parser;
|
||||||
|
http_parser_init(&parser, file_type);
|
||||||
|
size_t nparsed = http_parser_execute(&parser, &settings, data, file_length);
|
||||||
|
free(data);
|
||||||
|
|
||||||
|
if (nparsed != (size_t)file_length) {
|
||||||
|
fprintf(stderr,
|
||||||
|
"Error: %s (%s)\n",
|
||||||
|
http_errno_description(HTTP_PARSER_ERRNO(&parser)),
|
||||||
|
http_errno_name(HTTP_PARSER_ERRNO(&parser)));
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
44
ext/http-parser/contrib/url_parser.c
Normal file
44
ext/http-parser/contrib/url_parser.c
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#include "http_parser.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
dump_url (const char *url, const struct http_parser_url *u)
|
||||||
|
{
|
||||||
|
unsigned int i;
|
||||||
|
|
||||||
|
printf("\tfield_set: 0x%x, port: %u\n", u->field_set, u->port);
|
||||||
|
for (i = 0; i < UF_MAX; i++) {
|
||||||
|
if ((u->field_set & (1 << i)) == 0) {
|
||||||
|
printf("\tfield_data[%u]: unset\n", i);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("\tfield_data[%u]: off: %u len: %u part: \"%.*s\n",
|
||||||
|
i,
|
||||||
|
u->field_data[i].off,
|
||||||
|
u->field_data[i].len,
|
||||||
|
u->field_data[i].len,
|
||||||
|
url + u->field_data[i].off);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char ** argv) {
|
||||||
|
if (argc != 3) {
|
||||||
|
printf("Syntax : %s connect|get url\n", argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
struct http_parser_url u;
|
||||||
|
int len = strlen(argv[2]);
|
||||||
|
int connect = strcmp("connect", argv[1]) == 0 ? 1 : 0;
|
||||||
|
printf("Parsing %s, connect %d\n", argv[2], connect);
|
||||||
|
|
||||||
|
int result = http_parser_parse_url(argv[2], len, connect, &u);
|
||||||
|
if (result != 0) {
|
||||||
|
printf("Parse error : %d\n", result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
printf("Parse ok, result : \n");
|
||||||
|
dump_url(argv[2], &u);
|
||||||
|
return 0;
|
||||||
|
}
|
2175
ext/http-parser/http_parser.c
Normal file
2175
ext/http-parser/http_parser.c
Normal file
File diff suppressed because it is too large
Load Diff
111
ext/http-parser/http_parser.gyp
Normal file
111
ext/http-parser/http_parser.gyp
Normal file
@ -0,0 +1,111 @@
|
|||||||
|
# This file is used with the GYP meta build system.
|
||||||
|
# http://code.google.com/p/gyp/
|
||||||
|
# To build try this:
|
||||||
|
# svn co http://gyp.googlecode.com/svn/trunk gyp
|
||||||
|
# ./gyp/gyp -f make --depth=`pwd` http_parser.gyp
|
||||||
|
# ./out/Debug/test
|
||||||
|
{
|
||||||
|
'target_defaults': {
|
||||||
|
'default_configuration': 'Debug',
|
||||||
|
'configurations': {
|
||||||
|
# TODO: hoist these out and put them somewhere common, because
|
||||||
|
# RuntimeLibrary MUST MATCH across the entire project
|
||||||
|
'Debug': {
|
||||||
|
'defines': [ 'DEBUG', '_DEBUG' ],
|
||||||
|
'cflags': [ '-Wall', '-Wextra', '-O0', '-g', '-ftrapv' ],
|
||||||
|
'msvs_settings': {
|
||||||
|
'VCCLCompilerTool': {
|
||||||
|
'RuntimeLibrary': 1, # static debug
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'Release': {
|
||||||
|
'defines': [ 'NDEBUG' ],
|
||||||
|
'cflags': [ '-Wall', '-Wextra', '-O3' ],
|
||||||
|
'msvs_settings': {
|
||||||
|
'VCCLCompilerTool': {
|
||||||
|
'RuntimeLibrary': 0, # static release
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'msvs_settings': {
|
||||||
|
'VCCLCompilerTool': {
|
||||||
|
},
|
||||||
|
'VCLibrarianTool': {
|
||||||
|
},
|
||||||
|
'VCLinkerTool': {
|
||||||
|
'GenerateDebugInformation': 'true',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'conditions': [
|
||||||
|
['OS == "win"', {
|
||||||
|
'defines': [
|
||||||
|
'WIN32'
|
||||||
|
],
|
||||||
|
}]
|
||||||
|
],
|
||||||
|
},
|
||||||
|
|
||||||
|
'targets': [
|
||||||
|
{
|
||||||
|
'target_name': 'http_parser',
|
||||||
|
'type': 'static_library',
|
||||||
|
'include_dirs': [ '.' ],
|
||||||
|
'direct_dependent_settings': {
|
||||||
|
'defines': [ 'HTTP_PARSER_STRICT=0' ],
|
||||||
|
'include_dirs': [ '.' ],
|
||||||
|
},
|
||||||
|
'defines': [ 'HTTP_PARSER_STRICT=0' ],
|
||||||
|
'sources': [ './http_parser.c', ],
|
||||||
|
'conditions': [
|
||||||
|
['OS=="win"', {
|
||||||
|
'msvs_settings': {
|
||||||
|
'VCCLCompilerTool': {
|
||||||
|
# Compile as C++. http_parser.c is actually C99, but C++ is
|
||||||
|
# close enough in this case.
|
||||||
|
'CompileAs': 2,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}]
|
||||||
|
],
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
'target_name': 'http_parser_strict',
|
||||||
|
'type': 'static_library',
|
||||||
|
'include_dirs': [ '.' ],
|
||||||
|
'direct_dependent_settings': {
|
||||||
|
'defines': [ 'HTTP_PARSER_STRICT=1' ],
|
||||||
|
'include_dirs': [ '.' ],
|
||||||
|
},
|
||||||
|
'defines': [ 'HTTP_PARSER_STRICT=1' ],
|
||||||
|
'sources': [ './http_parser.c', ],
|
||||||
|
'conditions': [
|
||||||
|
['OS=="win"', {
|
||||||
|
'msvs_settings': {
|
||||||
|
'VCCLCompilerTool': {
|
||||||
|
# Compile as C++. http_parser.c is actually C99, but C++ is
|
||||||
|
# close enough in this case.
|
||||||
|
'CompileAs': 2,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}]
|
||||||
|
],
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
'target_name': 'test-nonstrict',
|
||||||
|
'type': 'executable',
|
||||||
|
'dependencies': [ 'http_parser' ],
|
||||||
|
'sources': [ 'test.c' ]
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
'target_name': 'test-strict',
|
||||||
|
'type': 'executable',
|
||||||
|
'dependencies': [ 'http_parser_strict' ],
|
||||||
|
'sources': [ 'test.c' ]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
304
ext/http-parser/http_parser.h
Normal file
304
ext/http-parser/http_parser.h
Normal file
@ -0,0 +1,304 @@
|
|||||||
|
/* 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
|
||||||
|
|
||||||
|
#define HTTP_PARSER_VERSION_MAJOR 2
|
||||||
|
#define HTTP_PARSER_VERSION_MINOR 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 */
|
||||||
|
#define HTTP_MAX_HEADER_SIZE (80*1024)
|
||||||
|
|
||||||
|
|
||||||
|
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) \
|
||||||
|
|
||||||
|
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_status_complete, "the on_status_complete 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") \
|
||||||
|
\
|
||||||
|
/* 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 char type : 2; /* enum http_parser_type */
|
||||||
|
unsigned char flags : 6; /* F_* values from 'flags' enum; semi-public */
|
||||||
|
unsigned char state; /* enum state from http_parser.c */
|
||||||
|
unsigned char header_state; /* enum header_state from http_parser.c */
|
||||||
|
unsigned char index; /* 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 short status_code; /* responses only */
|
||||||
|
unsigned char method; /* requests only */
|
||||||
|
unsigned char 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 char 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_cb on_status_complete;
|
||||||
|
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];
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
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
|
3425
ext/http-parser/test.c
Normal file
3425
ext/http-parser/test.c
Normal file
File diff suppressed because it is too large
Load Diff
323
node/Http.cpp
Normal file
323
node/Http.cpp
Normal file
@ -0,0 +1,323 @@
|
|||||||
|
/*
|
||||||
|
* ZeroTier One - Global Peer to Peer Ethernet
|
||||||
|
* Copyright (C) 2012-2013 ZeroTier Networks LLC
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* --
|
||||||
|
*
|
||||||
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||||
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||||
|
*
|
||||||
|
* If you would like to embed ZeroTier into a commercial application or
|
||||||
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||||
|
* LLC. Start here: http://www.zerotier.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <netdb.h>
|
||||||
|
#include <vector>
|
||||||
|
#include <set>
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
#include <unistd.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include "Http.hpp"
|
||||||
|
#include "Utils.hpp"
|
||||||
|
#include "InetAddress.hpp"
|
||||||
|
|
||||||
|
static http_parser_settings _http_parser_settings;
|
||||||
|
|
||||||
|
namespace ZeroTier {
|
||||||
|
|
||||||
|
static bool _sendAll(int fd,const void *buf,unsigned int len)
|
||||||
|
{
|
||||||
|
for(;;) {
|
||||||
|
int n = (int)::send(fd,buf,len,0);
|
||||||
|
if ((n < 0)&&(errno == EINTR))
|
||||||
|
continue;
|
||||||
|
return (n == (int)len);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::map<std::string,std::string> Http::EMPTY_HEADERS;
|
||||||
|
|
||||||
|
Http::Request::Request(
|
||||||
|
Http::Method m,
|
||||||
|
const std::string &url,
|
||||||
|
const std::map<std::string,std::string> &rh,
|
||||||
|
const std::string &rb,
|
||||||
|
bool (*handler)(Request *,void *,const std::string &,int,const std::map<std::string,std::string> &,const std::string &),
|
||||||
|
void *arg) :
|
||||||
|
_url(url),
|
||||||
|
_requestHeaders(rh),
|
||||||
|
_handler(handler),
|
||||||
|
_arg(arg),
|
||||||
|
_method(m),
|
||||||
|
_fd(0)
|
||||||
|
{
|
||||||
|
_http_parser_settings.on_message_begin = &Http::Request::_http_on_message_begin;
|
||||||
|
_http_parser_settings.on_url = &Http::Request::_http_on_url;
|
||||||
|
_http_parser_settings.on_status_complete = &Http::Request::_http_on_status_complete;
|
||||||
|
_http_parser_settings.on_header_field = &Http::Request::_http_on_header_field;
|
||||||
|
_http_parser_settings.on_header_value = &Http::Request::_http_on_header_value;
|
||||||
|
_http_parser_settings.on_headers_complete = &Http::Request::_http_on_headers_complete;
|
||||||
|
_http_parser_settings.on_body = &Http::Request::_http_on_body;
|
||||||
|
_http_parser_settings.on_message_complete = &Http::Request::_http_on_message_complete;
|
||||||
|
|
||||||
|
start();
|
||||||
|
}
|
||||||
|
|
||||||
|
Http::Request::~Request()
|
||||||
|
{
|
||||||
|
if (_fd > 0)
|
||||||
|
::close(_fd);
|
||||||
|
join();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Http::Request::main()
|
||||||
|
throw()
|
||||||
|
{
|
||||||
|
char buf[131072];
|
||||||
|
|
||||||
|
try {
|
||||||
|
http_parser_init(&_parser,HTTP_RESPONSE);
|
||||||
|
_parser.data = this;
|
||||||
|
|
||||||
|
http_parser_url urlParsed;
|
||||||
|
if (http_parser_parse_url(_url.c_str(),_url.length(),0,&urlParsed)) {
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"URL parse error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!(urlParsed.field_set & (1 << UF_SCHEMA))) {
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"URL specifies no schema");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string schema(_url.substr(urlParsed.field_data[UF_SCHEMA].off,urlParsed.field_data[UF_SCHEMA].len));
|
||||||
|
|
||||||
|
if (schema == "file") {
|
||||||
|
const std::string filePath(_url.substr(urlParsed.field_data[UF_PATH].off,urlParsed.field_data[UF_PATH].len));
|
||||||
|
|
||||||
|
uint64_t lm = Utils::getLastModified(filePath.c_str());
|
||||||
|
if (lm) {
|
||||||
|
const std::map<std::string,std::string>::const_iterator ifModSince(_requestHeaders.find("If-Modified-Since"));
|
||||||
|
if ((ifModSince != _requestHeaders.end())&&(ifModSince->second.length())) {
|
||||||
|
uint64_t t64 = Utils::fromRfc1123(ifModSince->second);
|
||||||
|
if ((t64)&&(lm > t64)) {
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,304,_responseHeaders,"");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Utils::readFile(filePath.c_str(),_responseBody)) {
|
||||||
|
_responseHeaders["Last-Modified"] = Utils::toRfc1123(lm);
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,200,_responseHeaders,_responseBody);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,404,_responseHeaders,"file not found or not readable");
|
||||||
|
return;
|
||||||
|
} else if (schema == "http") {
|
||||||
|
if (!(urlParsed.field_set & (1 << UF_HOST))) {
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"URL contains no host");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
std::string host(_url.substr(urlParsed.field_data[UF_HOST].off,urlParsed.field_data[UF_HOST].len));
|
||||||
|
|
||||||
|
std::list<InetAddress> v4,v6;
|
||||||
|
{
|
||||||
|
struct addrinfo *res = (struct addrinfo *)0;
|
||||||
|
if (!getaddrinfo(host.c_str(),(const char *)0,(const struct addrinfo *)0,&res)) {
|
||||||
|
struct addrinfo *p = res;
|
||||||
|
do {
|
||||||
|
if (p->ai_family == AF_INET)
|
||||||
|
v4.push_back(InetAddress(p->ai_addr));
|
||||||
|
else if (p->ai_family == AF_INET6)
|
||||||
|
v6.push_back(InetAddress(p->ai_addr));
|
||||||
|
} while ((p = p->ai_next));
|
||||||
|
freeaddrinfo(res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::list<InetAddress> *addrList;
|
||||||
|
if (v4.empty()&&v6.empty()) {
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"could not find address for host in URL");
|
||||||
|
return;
|
||||||
|
} else if (v4.empty()) {
|
||||||
|
addrList = &v6;
|
||||||
|
} else {
|
||||||
|
addrList = &v4;
|
||||||
|
}
|
||||||
|
InetAddress *addr;
|
||||||
|
{
|
||||||
|
addrList->sort();
|
||||||
|
addrList->unique();
|
||||||
|
unsigned int i = 0,k = 0;
|
||||||
|
k = rand() % addrList->size();
|
||||||
|
std::list<InetAddress>::iterator a(addrList->begin());
|
||||||
|
while (i++ != k) ++a;
|
||||||
|
addr = &(*a);
|
||||||
|
}
|
||||||
|
|
||||||
|
int remotePort = ((urlParsed.field_set & (1 << UF_PORT))&&(urlParsed.port)) ? (int)urlParsed.port : (int)80;
|
||||||
|
if ((remotePort <= 0)||(remotePort > 0xffff)) {
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"URL port out of range");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
addr->setPort(remotePort);
|
||||||
|
|
||||||
|
_fd = socket(addr->isV6() ? AF_INET6 : AF_INET,SOCK_STREAM,0);
|
||||||
|
if (_fd <= 0) {
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"could not open socket");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(;;) {
|
||||||
|
if (connect(_fd,addr->saddr(),addr->saddrLen())) {
|
||||||
|
if (errno == EINTR)
|
||||||
|
continue;
|
||||||
|
::close(_fd); _fd = 0;
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"connection failed to remote host");
|
||||||
|
return;
|
||||||
|
} else break;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *mstr = "GET";
|
||||||
|
switch(_method) {
|
||||||
|
case HTTP_METHOD_HEAD: mstr = "HEAD"; break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
int mlen = (int)snprintf(buf,sizeof(buf),"%s %s HTTP/1.1\r\nAccept-Encoding: \r\nHost: %s\r\n",mstr,_url.substr(urlParsed.field_data[UF_PATH].off,urlParsed.field_data[UF_PATH].len).c_str(),host.c_str());
|
||||||
|
if (mlen >= (int)sizeof(buf)) {
|
||||||
|
::close(_fd); _fd = 0;
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"URL too long");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!_sendAll(_fd,buf,mlen)) {
|
||||||
|
::close(_fd); _fd = 0;
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"write error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(std::map<std::string,std::string>::const_iterator rh(_requestHeaders.begin());rh!=_requestHeaders.end();++rh) {
|
||||||
|
mlen = (int)snprintf(buf,sizeof(buf),"%s: %s\r\n",rh->first.c_str(),rh->second.c_str());
|
||||||
|
if (mlen >= (int)sizeof(buf)) {
|
||||||
|
::close(_fd); _fd = 0;
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"header too long");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!_sendAll(_fd,buf,mlen)) {
|
||||||
|
::close(_fd); _fd = 0;
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"write error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!_sendAll(_fd,"\r\n",2)) {
|
||||||
|
::close(_fd); _fd = 0;
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"write error");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_responseStatusCode = 0;
|
||||||
|
_messageComplete = false;
|
||||||
|
for(;;) {
|
||||||
|
mlen = (int)::recv(_fd,buf,sizeof(buf),0);
|
||||||
|
if (mlen < 0) {
|
||||||
|
if (errno != EINTR)
|
||||||
|
break;
|
||||||
|
else continue;
|
||||||
|
}
|
||||||
|
if (((int)http_parser_execute(&_parser,&_http_parser_settings,buf,mlen) != mlen)||(_parser.upgrade)) {
|
||||||
|
::close(_fd); _fd = 0;
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"invalid HTTP response from server");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (_messageComplete) {
|
||||||
|
::close(_fd); _fd = 0;
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,_responseStatusCode,_responseHeaders,_responseBody);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
::close(_fd); _fd = 0;
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"empty HTTP response from server");
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"only 'file' and 'http' methods are supported");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
} catch ( ... ) {
|
||||||
|
suicidalThread = !_handler(this,_arg,_url,0,_responseHeaders,"unexpected exception retrieving URL");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Http::Request::_http_on_message_begin(http_parser *parser)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int Http::Request::_http_on_url(http_parser *parser,const char *data,size_t length)
|
||||||
|
{
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int Http::Request::_http_on_status_complete(http_parser *parser)
|
||||||
|
{
|
||||||
|
Http::Request *r = (Http::Request *)parser->data;
|
||||||
|
r->_responseStatusCode = parser->status_code;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int Http::Request::_http_on_header_field(http_parser *parser,const char *data,size_t length)
|
||||||
|
{
|
||||||
|
Http::Request *r = (Http::Request *)parser->data;
|
||||||
|
if ((r->_currentHeaderField.length())&&(r->_responseHeaders.find(r->_currentHeaderField) != r->_responseHeaders.end()))
|
||||||
|
r->_currentHeaderField.assign("");
|
||||||
|
r->_currentHeaderField.append(data,length);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int Http::Request::_http_on_header_value(http_parser *parser,const char *data,size_t length)
|
||||||
|
{
|
||||||
|
Http::Request *r = (Http::Request *)parser->data;
|
||||||
|
if (r->_currentHeaderField.length())
|
||||||
|
r->_responseHeaders[r->_currentHeaderField].append(data,length);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int Http::Request::_http_on_headers_complete(http_parser *parser)
|
||||||
|
{
|
||||||
|
Http::Request *r = (Http::Request *)parser->data;
|
||||||
|
return ((r->_method == Http::HTTP_METHOD_HEAD) ? 1 : 0);
|
||||||
|
}
|
||||||
|
int Http::Request::_http_on_body(http_parser *parser,const char *data,size_t length)
|
||||||
|
{
|
||||||
|
Http::Request *r = (Http::Request *)parser->data;
|
||||||
|
r->_responseBody.append(data,length);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int Http::Request::_http_on_message_complete(http_parser *parser)
|
||||||
|
{
|
||||||
|
Http::Request *r = (Http::Request *)parser->data;
|
||||||
|
r->_messageComplete = true;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace ZeroTier
|
129
node/Http.hpp
Normal file
129
node/Http.hpp
Normal file
@ -0,0 +1,129 @@
|
|||||||
|
/*
|
||||||
|
* ZeroTier One - Global Peer to Peer Ethernet
|
||||||
|
* Copyright (C) 2012-2013 ZeroTier Networks LLC
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* --
|
||||||
|
*
|
||||||
|
* ZeroTier may be used and distributed under the terms of the GPLv3, which
|
||||||
|
* are available at: http://www.gnu.org/licenses/gpl-3.0.html
|
||||||
|
*
|
||||||
|
* If you would like to embed ZeroTier into a commercial application or
|
||||||
|
* redistribute it in a modified binary form, please contact ZeroTier Networks
|
||||||
|
* LLC. Start here: http://www.zerotier.com/
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _ZT_HTTP_HPP
|
||||||
|
#define _ZT_HTTP_HPP
|
||||||
|
|
||||||
|
#include <map>
|
||||||
|
#include <string>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include "Thread.hpp"
|
||||||
|
|
||||||
|
#include "../ext/http-parser/http_parser.h"
|
||||||
|
|
||||||
|
namespace ZeroTier {
|
||||||
|
|
||||||
|
class Http
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* HTTP request methods
|
||||||
|
*/
|
||||||
|
enum Method
|
||||||
|
{
|
||||||
|
HTTP_METHOD_GET,
|
||||||
|
HTTP_METHOD_HEAD
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An empty headers map for convenience
|
||||||
|
*/
|
||||||
|
static const std::map<std::string,std::string> EMPTY_HEADERS;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* HTTP request
|
||||||
|
*/
|
||||||
|
class Request : protected Thread
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
/**
|
||||||
|
* Create and issue an HTTP request
|
||||||
|
*
|
||||||
|
* The supplied handler is called when the request is
|
||||||
|
* complete or if an error occurs. A code of zero indicates
|
||||||
|
* that the server could not be reached, and a description
|
||||||
|
* of the error will be in 'body'. If the handler returns
|
||||||
|
* false the Request object deletes itself. Otherwise the
|
||||||
|
* object must be deleted by other code.
|
||||||
|
*
|
||||||
|
* @param m Request method
|
||||||
|
* @param url Destination URL
|
||||||
|
* @param rh Request headers
|
||||||
|
* @param rb Request body or empty string for none (currently unused)
|
||||||
|
* @param handler Request handler function
|
||||||
|
* @param arg First argument to request handler
|
||||||
|
*/
|
||||||
|
Request(
|
||||||
|
Http::Method m,
|
||||||
|
const std::string &url,
|
||||||
|
const std::map<std::string,std::string> &rh,
|
||||||
|
const std::string &rb,
|
||||||
|
bool (*handler)(Request *,void *,const std::string &,int,const std::map<std::string,std::string> &,const std::string &),
|
||||||
|
void *arg);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Destruction cancels any in-progress request
|
||||||
|
*/
|
||||||
|
virtual ~Request();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
virtual void main()
|
||||||
|
throw();
|
||||||
|
|
||||||
|
private:
|
||||||
|
// HTTP parser handlers
|
||||||
|
static int _http_on_message_begin(http_parser *parser);
|
||||||
|
static int _http_on_url(http_parser *parser,const char *data,size_t length);
|
||||||
|
static int _http_on_status_complete(http_parser *parser);
|
||||||
|
static int _http_on_header_field(http_parser *parser,const char *data,size_t length);
|
||||||
|
static int _http_on_header_value(http_parser *parser,const char *data,size_t length);
|
||||||
|
static int _http_on_headers_complete(http_parser *parser);
|
||||||
|
static int _http_on_body(http_parser *parser,const char *data,size_t length);
|
||||||
|
static int _http_on_message_complete(http_parser *parser);
|
||||||
|
|
||||||
|
http_parser _parser;
|
||||||
|
std::string _url;
|
||||||
|
|
||||||
|
std::map<std::string,std::string> _requestHeaders;
|
||||||
|
std::map<std::string,std::string> _responseHeaders;
|
||||||
|
|
||||||
|
std::string _currentHeaderField;
|
||||||
|
std::string _responseBody;
|
||||||
|
|
||||||
|
bool (*_handler)(Request *,void *,const std::string &,int,const std::map<std::string,std::string> &,const std::string &);
|
||||||
|
void *_arg;
|
||||||
|
|
||||||
|
Http::Method _method;
|
||||||
|
int _responseStatusCode;
|
||||||
|
bool _messageComplete;
|
||||||
|
volatile int _fd;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace ZeroTier
|
||||||
|
|
||||||
|
#endif
|
@ -1,4 +1,5 @@
|
|||||||
OBJS=\
|
OBJS=\
|
||||||
|
ext/http-parser/http_parser.o \
|
||||||
ext/kissdb/kissdb.o \
|
ext/kissdb/kissdb.o \
|
||||||
ext/lz4/lz4hc.o \
|
ext/lz4/lz4hc.o \
|
||||||
ext/lz4/lz4.o \
|
ext/lz4/lz4.o \
|
||||||
@ -8,6 +9,7 @@ OBJS=\
|
|||||||
node/EthernetTap.o \
|
node/EthernetTap.o \
|
||||||
node/Filter.o \
|
node/Filter.o \
|
||||||
node/HMAC.o \
|
node/HMAC.o \
|
||||||
|
node/Http.o \
|
||||||
node/Identity.o \
|
node/Identity.o \
|
||||||
node/InetAddress.o \
|
node/InetAddress.o \
|
||||||
node/Logger.o \
|
node/Logger.o \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user