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; }
Request::Request() { _parse_part = PARSE_REQ_LINE; _total_req_size = 0; _last_was_value = true; // add new field for first _parse_err = 0; http_parser_settings_init(&_settings); _settings.on_url = ss_on_url; _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 = ss_on_message_complete; http_parser_init(&_parser, HTTP_REQUEST); _parser.data = this; }
http_parser_settings HttpServerRequest::Priv::httpSettings() { http_parser_settings settings; http_parser_settings_init(&settings); settings.on_message_begin = Tufao::HttpServerRequest::Priv::on_message_begin; settings.on_url = Tufao::HttpServerRequest::Priv::on_url; settings.on_header_field = Tufao::HttpServerRequest::Priv::on_header_field; settings.on_header_value = Tufao::HttpServerRequest::Priv::on_header_value; settings.on_headers_complete = Tufao::HttpServerRequest::Priv::on_headers_complete; settings.on_body = Tufao::HttpServerRequest::Priv::on_body; settings.on_message_complete = Tufao::HttpServerRequest::Priv::on_message_complete; return settings; }
void Connection::Init(uv_loop_t& loop) { uv_tcp_init(&loop, &m_tcp); m_tcp.data = this; m_closing = false; m_shutdownReq.data = this; m_writeReq.data = this; m_writePending = false; m_writesQueued = 0; http_parser_init(&m_httpParser, HTTP_REQUEST); m_httpParser.data = this; http_parser_settings_init(&m_httpParserSettings); m_httpParserSettings.on_message_complete = OnHttpRequestEnd; m_keepAlive = true; }
http_connection ICACHE_FLASH_ATTR * http_new_connection(uint8_t in,struct espconn *conn){ int i; //Find empty connection in pool for (i=0; i<MAX_CONNECTIONS; i++) if (connection_poll[i].espConnection==NULL) break; if (i>=MAX_CONNECTIONS) { NODE_DBG("Connection pool overflow!"); if(conn!=NULL){ espconn_disconnect(conn); } return; } NODE_DBG("\nNew connection, conn=%p, pool slot %d", conn, i); if(conn!=NULL){ connection_poll[i].espConnection=conn; connection_poll[i].espConnection->reverse=&connection_poll[i]; } //allocate buffer connection_poll[i].output.buffer = (uint8_t *)os_zalloc(HTTP_BUFFER_SIZE); //zero headers again- for sanity int j=0; while(j<MAX_HEADERS){ if(connection_poll[i].headers[j].value!=NULL && (connection_poll[i].headers[j].value!=connection_poll[i].headers[j].key)){ os_free(connection_poll[i].headers[j].value); connection_poll[i].headers[j].value=NULL; } connection_poll[i].headers[j].key=NULL; j++; } //mark cgi as not done connection_poll[i].cgi.done=0; //free response buffer again http_reset_buffer(&connection_poll[i]); //init body connection_poll[i].body.len=0; connection_poll[i].body.save=0; connection_poll[i].body.data=NULL; //reset parser http_parser_settings_init(&(connection_poll[i].parser_settings)); connection_poll[i].parser_settings.on_message_begin=on_message_begin; connection_poll[i].parser_settings.on_url=on_url; connection_poll[i].parser_settings.on_header_field=on_header_field; connection_poll[i].parser_settings.on_header_value=on_header_value; connection_poll[i].parser_settings.on_headers_complete=on_headers_complete; connection_poll[i].parser_settings.on_body=on_body; connection_poll[i].parser_settings.on_message_complete=on_message_complete; //attach httpd connection to data (socket info) so we may retrieve it easily inside parser callbacks connection_poll[i].parser.data=(&connection_poll[i]); //init parser if(in){ http_parser_init(&(connection_poll[i].parser),HTTP_REQUEST); //register espconn callbacks espconn_regist_recvcb(conn, http_process_received_cb); espconn_regist_reconcb(conn, http_process_reconnect_cb); espconn_regist_disconcb(conn, http_process_disconnect_cb); espconn_regist_sentcb(conn, http_process_sent_cb); } else{ http_parser_init(&(connection_poll[i].parser),HTTP_RESPONSE); connection_poll[i].espConnection = &connection_poll[i].client_connection; connection_poll[i].espConnection->reverse=&connection_poll[i]; //set reverse object connection_poll[i].espConnection->type=ESPCONN_TCP; connection_poll[i].espConnection->state=ESPCONN_NONE; connection_poll[i].espConnection->proto.tcp = &connection_poll[i].client_tcp; //register espconn callbacks espconn_regist_recvcb(connection_poll[i].espConnection, http_process_received_cb); espconn_regist_reconcb(connection_poll[i].espConnection, http_process_reconnect_cb); espconn_regist_disconcb(connection_poll[i].espConnection, http_process_disconnect_cb); espconn_regist_sentcb(connection_poll[i].espConnection, http_process_sent_cb); } espconn_regist_time(conn,30,0); return &connection_poll[i]; }