/* Takes as arguments a string for type and a table for event callbacks */ static int lhttp_parser_new (lua_State *L) { const char *type = luaL_checkstring(L, 1); http_parser* parser; luaL_checktype(L, 2, LUA_TTABLE); parser = (http_parser*)lua_newuserdata(L, sizeof(http_parser)); if (0 == strcmp(type, "request")) { http_parser_init(parser, HTTP_REQUEST); } else if (0 == strcmp(type, "response")) { http_parser_init(parser, HTTP_RESPONSE); } else { return luaL_argerror(L, 1, "type must be 'request' or 'response'"); } /* Store the current lua state in the parser's data */ parser->data = L; /* Set the callback table as the userdata's environment */ lua_pushvalue(L, 2); lua_setfenv (L, -2); /* Set the type of the userdata as an lhttp_parser instance */ luaL_getmetatable(L, "lhttp_parser"); lua_setmetatable(L, -2); /* return the userdata */ return 1; }
void session_start(struct session *s){ debug("session_start"); http_parser_init(s->client_parser, HTTP_REQUEST); http_parser_init(s->server_parser, HTTP_RESPONSE); s->client_parser->data = s; s->server_parser->data = s; session_init(s); }
void streamClean(struct stream *s) { char buffer[1024]; http_parser_init(&(s->request_parser), HTTP_REQUEST); http_parser_init(&(s->response_parser), HTTP_RESPONSE); s->is_http = 0; s->request_data_size = 0; s->response_data_size = 0; s->json = json_object_new_object(); sprintf(buffer, "%s:%i", int_ntoa(s->addr.saddr), s->addr.source); json_object_object_add(s->json, "src", json_object_new_string(buffer)); sprintf(buffer, "%s:%i", int_ntoa(s->addr.daddr), s->addr.dest); json_object_object_add(s->json, "dst", json_object_new_string(buffer)); }
static int lhttp_parser_reinitialize (lua_State *L) { http_parser* parser = (http_parser *)luaL_checkudata(L, 1, "lhttp_parser"); const char *type = luaL_checkstring(L, 2); if (0 == strcmp(type, "request")) { http_parser_init(parser, HTTP_REQUEST); } else if (0 == strcmp(type, "response")) { http_parser_init(parser, HTTP_RESPONSE); } else { return luaL_argerror(L, 1, "type must be 'request' or 'response'"); } return 0; }
HttpParser::HttpParser() : calledHeaderValue_(false) { http_parser_init(&parser_, HTTP_REQUEST); parser_.data = this; // functions }
static http_parser http_dissector_get_parser() { http_parser parser; http_parser_init(&parser, HTTP_BOTH); return (parser); }
struct http_client * http_client_new(struct worker *w, int fd, in_addr_t addr) { struct http_client *c = calloc(1, sizeof(struct http_client)); c->fd = fd; c->w = w; c->addr = addr; c->s = w->s; /* parser */ http_parser_init(&c->parser, HTTP_REQUEST); c->parser.data = c; /* callbacks */ c->settings.on_url = http_client_on_url; c->settings.on_query_string = http_client_on_query_string; c->settings.on_body = http_client_on_body; c->settings.on_message_complete = http_client_on_message_complete; c->settings.on_header_field = http_client_on_header_name; c->settings.on_header_value = http_client_on_header_value; c->last_cb = LAST_CB_NONE; return c; }
http_request_t * http_request_new (http_socket_t* sock, const char *buf) { http_request_t *self = NULL; http_parser *parser = NULL; int nparsed = 0; size_t size = 0; // ensure alloc if (NULL == buf) { return NULL; } size = strlen(buf); // malloc self = (http_request_t *) malloc(sizeof(http_request_t)); if (NULL == self) { return NULL; } parser = (http_parser *) malloc(sizeof(http_parser)); if (NULL == parser) { return NULL; } // parser parser->data = self; http_parser_init(parser, HTTP_REQUEST); // init self->socket_ = sock; self->parser_ = parser; self->headers = hash_new(); // parse nparsed = http_parser_execute(parser, &settings, buf, size); return self; }
TEST(RequestTest, test_http_parser_post) { http_parser_settings settings; settings.on_message_begin = NULL; settings.on_url = request_url_cb; settings.on_status = on_status; settings.on_header_field = ss_on_header_field; settings.on_header_value = ss_on_header_value; settings.on_headers_complete = ss_on_headers_complete; settings.on_body = ss_on_body; settings.on_message_complete = on_message_complete; settings.on_chunk_header = NULL; settings.on_chunk_complete = NULL; http_parser parser; http_parser_init(&parser, HTTP_REQUEST); Request req; parser.data = &req; int nparsed = http_parser_execute(&parser, &settings, TEST_POST_REQ_LOW_LEN.c_str(), TEST_POST_REQ_LOW_LEN.size()); LOG_INFO("parsed size:%d, parser->upgrade:%d,total_size:%u", nparsed, parser.upgrade, TEST_POST_REQ_LOW_LEN.size()); if (parser.http_errno) { LOG_INFO("ERROR:%s", http_errno_description(HTTP_PARSER_ERRNO(&parser))); } ASSERT_EQ(0, (int)parser.http_errno); }
int util_parse_http_response(char *buf, size_t len) { http_parser parser; http_parser_settings settings = { .on_message_complete = util_message_complete_cb }; if (len < 0) return -1; http_message_done = 0; char *r = buf; #ifdef MVS r = clax_etoa_alloc(buf, len); #endif http_parser_init(&parser, HTTP_RESPONSE); int ret = http_parser_execute(&parser, &settings, r, len); #ifdef MVS free(r); #endif return ret == len && http_message_done; }
void tcp_new_connection_cb(uv_stream_t* server, int status) { /* initialize a new http http_request struct */ http_request_t* http_request = malloc(sizeof(http_request_t)); if (status == -1) { fprintf(stderr, "Error on connection: %s.\n", uv_strerror(uv_last_error(loop))); } /* create an extra tcp handle for the http_request */ uv_tcp_init(loop, (uv_tcp_t*) &http_request->stream); /* set references so we can use our http_request in http_parser and libuv */ http_request->stream.data = http_request; http_request->parser.data = http_request; /* accept the created http_request */ if (uv_accept(server, &http_request->stream) == 0) { /* initialize our http parser */ http_parser_init(&http_request->parser, HTTP_REQUEST); /* start reading from the tcp http_request socket */ uv_read_start(&http_request->stream, alloc_buffer, tcp_read_cb); } else { /* we seem to have an error and quit */ uv_close((uv_handle_t*) &http_request->stream, NULL); } }
int test_http_parser2() { http_parser_settings settings; settings.on_message_begin = on_message_begin; settings.on_url = request_url_cb; settings.on_status = on_status; settings.on_header_field = header_field_cb; settings.on_header_value = header_value_cb; settings.on_headers_complete = on_headers_complete; settings.on_body = on_body; settings.on_message_complete = on_message_complete; settings.on_chunk_header = NULL; settings.on_chunk_complete = NULL; http_parser parser; http_parser_init(&parser, HTTP_REQUEST); Request req; parser.data = &req; int nparsed = http_parser_execute(&parser, &settings, TEST_POST_REQ1.c_str(), TEST_POST_REQ1.size()); LOG_INFO("parsed size:%d, parser->upgrade:%d,total_size:%u", nparsed, parser.upgrade, TEST_POST_REQ1.size()); if (parser.http_errno) { LOG_INFO("ERROR:%s", http_errno_description(HTTP_PARSER_ERRNO(&parser))); } nparsed = http_parser_execute(&parser, &settings, TEST_POST_REQ2.c_str(), TEST_POST_REQ2.size()); LOG_INFO("parsed size:%d, parser->upgrade:%d,total_size:%u", nparsed, parser.upgrade, TEST_POST_REQ2.size()); if (parser.http_errno) { LOG_INFO("ERROR:%s", http_errno_description(HTTP_PARSER_ERRNO(&parser))); } return 0; }
void moloch_http_finish( MolochConn_t *conn, gboolean sync) { char line[1000]; strcpy(line, conn->line); conn->hp_complete = 0; http_parser_init(&conn->parser, HTTP_RESPONSE); if (!sync && conn->request->data) { MOLOCH_SIZE_FREE(buffer, conn->request->data); conn->request->data = 0; } while (sync) { moloch_http_read_cb(0, 0, conn); if (conn->hp_complete) break; } gettimeofday(&conn->endTime, NULL); if (config.logESRequests) LOG("%s %ldms %ldms", line, (conn->sendTime.tv_sec - conn->startTime.tv_sec)*1000 + (conn->sendTime.tv_usec/1000 - conn->startTime.tv_usec/1000), (conn->endTime.tv_sec - conn->startTime.tv_sec)*1000 + (conn->endTime.tv_usec/1000 - conn->startTime.tv_usec/1000) ); }
struct bb_session_request *bb_new_request(struct bb_session *bbs) { struct bb_session_request *bbsr = malloc(sizeof(struct bb_session_request)); if (!bbsr) { perror("malloc()"); return NULL; } memset(bbsr, 0, sizeof(struct bb_session_request)); bbsr->bbs = bbs; http_parser_init(&bbsr->parser, HTTP_REQUEST); bbsr->parser.data = bbsr; bbsr->last_was_value = 1; bbsr->content_length = ULLONG_MAX; if (!bbs->requests_head) { //printf("first request\n"); bbs->requests_head = bbsr; bbs->requests_tail = bbsr; } else { bbs->requests_tail->next = bbsr; bbs->requests_tail = bbsr; } bbs->new_request = 0; return bbsr; }
parsed_request_t *parse_request(char* request) { //struktur in der die Callbacks ihre Daten ablegen parsed_request = malloc(sizeof(parsed_request)); if(NULL == parsed_request) { perror("Memory allocation for \"parsed_request\" failed"); exit(EXIT_FAILURE); } http_parser_settings settings = get_http_parser_callbacks(); http_parser parser; http_parser_init(&parser, HTTP_REQUEST); http_parser_execute(&parser, &settings, request, strlen(request)); if(parser.http_errno != 0) { fprintf(stderr, "Error while parsing. http_errno: %i\n", parser.http_errno); } parsed_request->method = parser.method; return parsed_request; }
StreamingResponseDecoder() : failure(false), header(HEADER_FIELD), response(NULL) { settings.on_message_begin = &StreamingResponseDecoder::on_message_begin; #if !(HTTP_PARSER_VERSION_MAJOR >=2) settings.on_path = &StreamingResponseDecoder::on_path; settings.on_fragment = &StreamingResponseDecoder::on_fragment; settings.on_query_string = &StreamingResponseDecoder::on_query_string; #endif settings.on_url = &StreamingResponseDecoder::on_url; settings.on_header_field = &StreamingResponseDecoder::on_header_field; settings.on_header_value = &StreamingResponseDecoder::on_header_value; settings.on_headers_complete = &StreamingResponseDecoder::on_headers_complete; settings.on_body = &StreamingResponseDecoder::on_body; settings.on_message_complete = &StreamingResponseDecoder::on_message_complete; http_parser_init(&parser, HTTP_RESPONSE); parser.data = this; }
void bolt_connection_keepalive(bolt_connection_t *c) { if (c->http_code == 200) { c->icache->refcount--; c->icache = NULL; } c->http_code = 200; c->recv_state = BOLT_HTTP_STATE_START; c->parse_field = BOLT_PARSE_FIELD_START; c->keepalive = 0; c->parse_error = 0; c->header_only = 0; c->rpos = c->rbuf; c->rlast = c->rbuf; c->headers.tms = 0; http_parser_init(&c->hp, HTTP_REQUEST); c->hp.data = c; bolt_connection_remove_wevent(c); bolt_connection_install_revent(c, bolt_connection_recv_handler); }
QHttpConnection::QHttpConnection(QTcpSocket *socket, QObject *parent) : QObject(parent), m_socket(socket), m_parser(0), m_parserSettings(0), m_request(0), m_transmitLen(0), m_transmitPos(0) { m_parser = (http_parser *)malloc(sizeof(http_parser)); http_parser_init(m_parser, HTTP_REQUEST); m_parserSettings = new http_parser_settings(); m_parserSettings->on_message_begin = MessageBegin; m_parserSettings->on_url = Url; m_parserSettings->on_header_field = HeaderField; m_parserSettings->on_header_value = HeaderValue; m_parserSettings->on_headers_complete = HeadersComplete; m_parserSettings->on_body = Body; m_parserSettings->on_message_complete = MessageComplete; m_parser->data = this; connect(socket, SIGNAL(readyRead()), this, SLOT(parseRequest())); connect(socket, SIGNAL(disconnected()), this, SLOT(socketDisconnected())); connect(socket, SIGNAL(bytesWritten(qint64)), this, SLOT(updateWriteCount(qint64))); }
char *test_parser_thrashing() { glob_t test_files; unsigned int i = 0; int nparsed = 0; int delta = 0; int tests_run = 0; int execs_run = 0; int unfinished = 0; int errors = 0; int rc = glob("tests/and_suite/*", 0, NULL, &test_files); mu_assert(rc == 0, "Failed to glob file sin tests/and_suite/*"); for(i = 0; i < test_files.gl_pathc; i++) { FILE *infile = fopen(test_files.gl_pathv[i], "r"); mu_assert(infile != NULL, "Failed to open test file."); bstring data = bread((bNread)fread, infile); fclose(infile); mu_assert(data != NULL, "Failed to read test file."); tests_run++; http_parser p = setup_parser(); nparsed = 0; delta = 0; while(nparsed < blength(data)) { debug("json PARSING: %d of %d at %s", nparsed, blength(data), bdataofs(data, nparsed)); delta = http_parser_execute(&p, bdata(data), blength(data), nparsed); execs_run++; if(delta == 0) { break; } if(!http_parser_finish(&p)) { unfinished++; } nparsed += delta; if(http_parser_has_error(&p)) { errors++; } debug("TEST %s results: delta %d, has_error: %d, is_finished: %d", test_files.gl_pathv[i], nparsed, http_parser_has_error(&p), http_parser_is_finished(&p)); http_parser_init(&p); // reset for the next try } } debug("HTTP PARSING: tests_run: %d, execs_run: %d, unfinished: %d, errors: %d", tests_run, execs_run, unfinished, errors); return NULL; }
/* parser indicates wrong http format, return -1. ask for close. * let's send back a response, return 0. * request not complete, return 1 */ int parse_request(struct request *req) { size_t nparsed; if (!req) { return -1; } if (!req->parser_inited) { http_parser_init(&req->parser, HTTP_REQUEST); req->parser_inited = 1; } set_cur_req(req); nparsed = http_parser_execute(&req->parser, &settings, req->in_http + req->in_http_cur, req->in_http_len - req->in_http_cur); /* if parser says wrong and it is not because us * XXX when we support keep-alive in the future, * we may break the parser intendedly */ if (nparsed != req->in_http_len - req->in_http_cur && !I_INFO(req).message_complete_cb_called) { req->in_http_err = 1; return -1; } req->in_http_cur += nparsed; if (I_INFO(req).message_complete_cb_called) { I_INFO(req).message_complete_cb_called = 0; return 0; } return 1; }
Request *requestNew(char *rbuf, size_t count) { Request *req = calloc(sizeof(Request), 1); int nparsed; static http_parser_settings settings = {.on_message_begin = message_begin_cb ,.on_header_field = header_field_cb ,.on_header_value = header_value_cb ,.on_path = request_path_cb ,.on_url = request_url_cb ,.on_fragment = fragment_cb ,.on_query_string = query_string_cb ,.on_body = body_cb ,.on_headers_complete = headers_complete_cb ,.on_message_complete = message_complete_cb }; parser = NULL; parser = malloc(sizeof(http_parser)); if (!parser) { fprintf(stderr, "FATAL: Failed to allocate parser."); exit(-1); } http_parser_init(parser, HTTP_REQUEST); parser->data = req; nparsed = http_parser_execute(parser, &settings, rbuf, count); return req; }
static void clear_parser_state(http_subtransport *t) { unsigned i; char *entry; http_parser_init(&t->parser, HTTP_RESPONSE); gitno_buffer_setup(&t->socket, &t->parse_buffer, t->parse_buffer_data, sizeof(t->parse_buffer_data)); t->last_cb = NONE; t->parse_error = 0; t->parse_finished = 0; git_buf_free(&t->parse_header_name); git_buf_init(&t->parse_header_name, 0); git_buf_free(&t->parse_header_value); git_buf_init(&t->parse_header_value, 0); git__free(t->content_type); t->content_type = NULL; git__free(t->location); t->location = NULL; git_vector_foreach(&t->www_authenticate, i, entry) git__free(entry); git_vector_free(&t->www_authenticate); }
net_http_handle *net_http_open(char *host, char *request) { struct net_http_handle *h = malloc(sizeof(struct net_http_handle)); h->socket = net_connect(host, 80); if (!h->socket) { free(h); return NULL; } int len = snprintf(h->buf, BUF_SIZE, "GET %s HTTP/1.1\r\n" "Host: %s\r\n" "Accept-Encoding:\r\n" "Connection: close\r\n\r\n", request, host); if (len >= BUF_SIZE || net_write(h->socket, h->buf, len) < len) { DBG_WARN("failed to send \"%s\" to %s", request, host); net_close(h->socket); free(h); return NULL; } h->data = NULL; h->len = 0; h->complete = 0; memset(&h->settings, 0, sizeof(http_parser_settings)); h->settings.on_body = on_body_cb; h->settings.on_message_complete = on_message_complete_cb; h->parser = malloc(sizeof(http_parser)); h->parser->data = h; http_parser_init(h->parser, HTTP_RESPONSE); return h; }
struct Client *make_client(int fd) { http_parser_settings *settings = malloc(sizeof(http_parser_settings)); http_parser *parser = malloc(sizeof(http_parser)); http_parser_settings_init(settings); http_parser_init(parser, HTTP_REQUEST); settings->on_message_complete = on_message_complete_cb; struct Client *c = malloc(sizeof(struct Client)); if (!c) { fprintf(stderr, "Couldn't allocate memory for connection %d\n", fd); exit(EXIT_FAILURE); } c->fd = fd; c->cstate = CONNECTED; c->pstate = IN_PROGRESS; c->to_reply = 0; memset(c->buf, 0, RECV_BUFFER); c->parser_settings = settings; c->parser = parser; c->parser->data = c; return c; }
static void on_connect(uv_stream_t* stream, int status) { MALLOC(http_connection_t, conn); get_unique_id(conn->id); conn->request = NULL; conn->response = NULL; conn->time = timems(); conn->routes = (struct http_routes *)stream->data; conn->timeout = NULL; conn->replied = 0; REFERENCE_INIT(conn, _conn_destroy); uv_tcp_init(uv_loop, &conn->stream); http_parser_init(&conn->parser, HTTP_REQUEST); conn->write_req.data = conn; conn->parser.data = conn; conn->stream.data = conn; /* TODO: Use the return values from uv_accept() and * uv_read_start() */ uv_accept(stream, (uv_stream_t*)&conn->stream); uv_read_start( (uv_stream_t*)&conn->stream, http_stream_on_alloc, http_stream_on_read ); }
int main(int argc , char *argv[]) { SOCKET s = net_init(); if(!s) return 1; int c = net_connect(s, "127.0.0.1", 80); if(c < 0) return 1; //Send some data char *message = netfile_read(); message = strcat(message, "\r\n\r\n"); char *buffer; buffer = net_send(s, message, buffer); // response //write response to file as raw netfile_write(buffer); //parse response size_t parsed; struct http_parser parser; http_parser_init(&parser, HTTP_RESPONSE); parsed = http_parser_execute(&parser, &settings, buffer, strlen(buffer)); // http global object printf("%d\n",http.count); printf("%s", http.status); for(int i=0; i<http.count; i++){ printf("\n%s: %s", http.headers[i].key, http.headers[i].value); } return 0; }
static void http_tokenizer_reset_internal(http_tokenizer* tokenizer) { http_parser* parser = &(tokenizer->parser); tokenizer->count = 0; http_parser_init(parser, parser->type); parser->data = NULL; }
static PyObject * HttpParserObject_reset(HttpParserObject* self) { Py_DECREF(self->environ); self->environ = PyDict_New(); http_parser_init(self->http); Py_RETURN_NONE; }
/** * call-seq: * parser.reset -> nil * * Resets the parser to it's initial state so that you can reuse it * rather than making new ones. */ VALUE HttpParser_reset(VALUE self) { http_parser *http = NULL; DATA_GET(self, http_parser, http); http_parser_init(http); return Qnil; }
void Pillow::HttpResponseParser::clear() { http_parser_init(&parser, HTTP_RESPONSE); while (!_headers.isEmpty()) _headers.pop_back(); _content.clear(); if (_parsing) pause(); // clear() got called from a callback. gracefully recover by pausing the parser. }