void ICACHE_FLASH_ATTR http_process_free_connection(http_connection *conn){ http_reset_buffer(conn); conn->espConnection=NULL; conn->cgi.function=NULL; conn->cgi.data=NULL; conn->cgi.argument=NULL; //free headers int j=0; while(j<MAX_HEADERS){ if(conn->headers[j].value!=NULL && (conn->headers[j].value!=conn->headers[j].key)) { os_free(conn->headers[j].value); conn->headers[j].value=NULL; } conn->headers[j].key=NULL; j++; } //free buffer os_free(conn->output.buffer); }
//ws int ICACHE_FLASH_ATTR http_websocket_HANDSHAKE(http_connection *c){ http_reset_buffer(c); int ret = http_write(c," HTTP/1.1 101 Switching Protocols") && http_end_line(c) && http_end_headers(c); }
//REQUEST int ICACHE_FLASH_ATTR http_request_start(http_connection *c,const char *method,const char *path){ http_reset_buffer(c); int ret = http_write(c,method) && http_write(c," ") && http_write(c,path) && http_write(c," HTTP/1.0") && http_end_line(c) && http_HEADER(c,HTTP_CONNECTION,"Close"); NODE_DBG("Request: %s",c->output.buffer); }
int ICACHE_FLASH_ATTR http_transmit(http_connection *c){ NODE_DBG("Transmit Buffer"); int len = (c->output.bufferPos - c->output.buffer); if(len>0 && len <= HTTP_BUFFER_SIZE){ espconn_send(c->espConnection, (uint8_t*)(c->output.buffer),len); } else{ NODE_DBG("Wrong transmit size %d",len); } //free buffer http_reset_buffer(c); return len; }
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]; }