//special callback like function to pass ws data to cgi static int ICACHE_FLASH_ATTR on_websocket_data(http_parser *parser, char *data, size_t length) { NODE_DBG("\non_websocket_data : "); int i; for(i=0;i<length;i++) os_printf("%02X",data[i]); os_printf("\r\n"); //grab the connection http_connection * conn = (http_connection *)parser->data; conn->state = HTTPD_STATE_WS_DATA; //set state conn->body.data = (char *)data; conn->body.len = length; //execute cgi again http_execute_cgi(conn); conn->body.data=NULL; conn->body.len=0; return 0; }
static int ICACHE_FLASH_ATTR on_url(http_parser *parser, const char *url, size_t length) { NODE_DBG("\nhttp_parser url: "); #ifdef DEVELOP_VERSION nprintf(url,length); #endif NODE_DBG("http_parser method: %d",parser->method); //grab the connection http_connection * conn = (http_connection *)parser->data; conn->state=HTTPD_STATE_ON_URL; //set state os_memcpy(conn->url,url,length); //copy url to connection info conn->url[length]=0; //null terminate string http_parse_url(conn); //execute cgi http_execute_cgi(conn); return 0; }
static void ICACHE_FLASH_ATTR http_process_disconnect_cb(void *arg) { NODE_DBG("Disconnect conn=%p",arg); http_connection *conn = http_process_find_connection(arg); if(conn!=NULL){ //is it a client connection? if(conn->espConnection== &conn->client_connection){ //tell parser about EOF http_parser_execute( &(conn->parser), &(conn->parser_settings), NULL, 0); } if (conn->parser.upgrade) { conn->state=HTTPD_STATE_WS_CLIENT_DISCONNECT; http_execute_cgi(conn); } http_process_free_connection(conn); } else{ //find connections that should be closed int i; for(i=0;i<MAX_CONNECTIONS;i++) { struct espconn *conn = connection_poll[i].espConnection; if(conn!=NULL){ if(conn->state==ESPCONN_NONE || conn->state >=ESPCONN_CLOSE){ //should close //is it a client connection? If yes, don't free if(&connection_poll[i].client_connection != connection_poll[i].espConnection) { http_process_free_connection(&connection_poll[i]); } } } } } }
static int ICACHE_FLASH_ATTR on_headers_complete(http_parser *parser){ NODE_DBG("\nhttp_parser headers complete"); //grab the connection http_connection * conn = (http_connection *)parser->data; conn->state = HTTPD_STATE_HEADERS_END; //set state //execute cgi again http_execute_cgi(conn); }
int ICACHE_FLASH_ATTR http_wifi_api_check_internet_cb(http_connection *c){ NODE_DBG("http_wifi_api_check_internet_cb state: %d",c->state); http_connection *request=c->reverse; if(request->espConnection==NULL) { //client request has been aborted return HTTP_CLIENT_CGI_DONE; } api_cgi_check_internet_status * status = (api_cgi_check_internet_status *)request->cgi.data; if(c->state==HTTP_CLIENT_DNS_NOT_FOUND){ status->state=3; http_execute_cgi(request); return HTTP_CLIENT_CGI_DONE; } //wait whole body if(c->state==HTTPD_STATE_BODY_END){ if(c->parser.status_code==200) status->state=2; else status->state=3; http_execute_cgi(request); return HTTP_CLIENT_CGI_DONE; } return HTTP_CLIENT_CGI_MORE; }
static int ICACHE_FLASH_ATTR on_status(http_parser *parser, const char *url, size_t length) { NODE_DBG("http_parser status: "); nprintf(url,length); //grab the connection http_connection * conn = (http_connection *)parser->data; conn->state=HTTPD_STATE_ON_STATUS; //set state //execute cgi again http_execute_cgi(conn); return 0; }
static int ICACHE_FLASH_ATTR on_body(http_parser *parser, const char *at, size_t length) { NODE_DBG("\nhttp_parser body: "); #ifdef DEVELOP_VERSION nprintf(at,length); #endif //grab the connection http_connection * conn = (http_connection *)parser->data; conn->state = HTTPD_STATE_ON_BODY; //set state if(conn->body.save){ if(conn->body.data==NULL){ NODE_DBG("saving body len %d",length); conn->body.data = (char *) os_malloc(length+1); os_memcpy(conn->body.data,at,length); conn->body.len = length; conn->body.data[length]=0; } else{ //assuming body can come in different tcp packets, this callback will be called //more than once NODE_DBG("appending body len %d",length); size_t newLenght = conn->body.len+length; char * newBuffer = (char *) os_malloc(newLenght+1); os_memcpy(newBuffer,conn->body.data,conn->body.len); //copy previous data os_memcpy(newBuffer+conn->body.len,at,length); //copy new data os_free(conn->body.data); //free previous conn->body.data=newBuffer; conn->body.len=newLenght; conn->body.data[newLenght]=0; } } //execute cgi again http_execute_cgi(conn); return 0; }
static int ICACHE_FLASH_ATTR on_message_complete(http_parser *parser){ NODE_DBG("\nhttp_parser message complete"); //grab the connection http_connection * conn = (http_connection *)parser->data; conn->state = HTTPD_STATE_BODY_END; //set state //execute cgi again http_execute_cgi(conn); //free body if(conn->body.save==1 && conn->body.data!=NULL){ NODE_DBG("freeing body memory"); os_free(conn->body.data); conn->body.len=0; } return 0; }
//esp conn callbacks static void ICACHE_FLASH_ATTR http_process_sent_cb(void *arg) { NODE_DBG("\nhttp_process_sent_cb, conn %p",arg); http_connection *conn = http_process_find_connection(arg); if(conn==NULL) return; if (conn->cgi.done==1) { //Marked for destruction? NODE_DBG("Conn %p is done. Closing.", conn->espConnection); espconn_disconnect(conn->espConnection); http_process_free_connection(conn); return; //No need to execute cgi again }// if(conn->parser.upgrade){ conn->state = HTTPD_STATE_WS_DATA_SENT; //set state } http_execute_cgi(conn); }