int ICACHE_FLASH_ATTR http_write(http_connection *c,const char * message){ size_t len = strlen(message); return http_nwrite(c,message,len); }
// If a request to transmit data overflows the send buffer, the cgi function will be temporarely // replaced by this one and later restored when all data is sent. int cgi_transmit(http_connection *connData) { CGI_DBG("cgi_transmit\n"); struct cgi_transmit_arg *arg = (struct cgi_transmit_arg*)connData->cgi.data; if (arg->len > 0) { CGI_DBG("cgi_transmit %d bytes\n",arg->len); int rem = connData->output.buffer + HTTP_BUFFER_SIZE - connData->output.bufferPos; int bytesToWrite = rem; if(arg->len < rem ) { bytesToWrite = arg->len; } http_nwrite(connData,arg->dataPos,bytesToWrite); arg->len -= bytesToWrite; arg->dataPos+=bytesToWrite; } //all written if(arg->len==0) { //free data os_free(arg->data); //copy old cgi back memcpy(&connData->cgi,&arg->previous_cgi,sizeof(cgi_struct)); //free cgi arg os_free(arg); } return HTTPD_CGI_MORE; }
int ICACHE_FLASH_ATTR write_json_string(http_connection *c,const char *s){ http_write(c,"\""); if(s!=NULL){ while(*s!='\0'){ if(*s=='"') http_write(c,"\\"); http_nwrite(c,s,1); s++; } } return http_write(c,"\""); }
//cgi for static file serving int ICACHE_FLASH_ATTR cgi_file_system(http_connection *connData) { cgi_fs_state *state = (cgi_fs_state *)connData->cgi.data; int len; char buff[HTTP_BUFFER_SIZE]; //wait for body end state if(connData->state<HTTPD_STATE_BODY_END) return HTTPD_CGI_NEXT_RULE; if (state==NULL) { //First call to this cgi. Open the file so we can read it. RO_FILE *f; if( ! connData->url_parsed.field_set & (1<<UF_PATH) ){ //there's no PATH on the url ( WTF? ), so not found return HTTPD_CGI_NOTFOUND; } char * path = http_url_get_path(connData); f=f_open(path); if (f==NULL) { return HTTPD_CGI_NOTFOUND; } NODE_DBG("File %s opened",path); state = (cgi_fs_state*)os_zalloc(sizeof(cgi_fs_state)); state->f = f; connData->cgi.data=state; //save state for next time //set headers http_SET_HEADER(connData,HTTP_CONTENT_TYPE,http_get_mime(connData->url)); http_SET_HEADER(connData,HTTP_CACHE_CONTROL,HTTP_DEFAULT_CACHE); http_SET_CONTENT_LENGTH(connData,f->file->size); if(f->file->gzip) http_SET_HEADER(connData,HTTP_CONTENT_ENCODING,"gzip"); http_response_OK(connData); return HTTPD_CGI_MORE; } else{ //file found, transmit data len=f_read(state->f, buff, HTTP_BUFFER_SIZE); if (len>0){ NODE_DBG("Sending %d bytes of data",len); http_nwrite(connData,(const char*)buff,len); //http_response_transmit(connData); } if (state->f->eof) { NODE_DBG("End of file reached"); //We're done. f_close(state->f); os_free(state); return HTTPD_CGI_DONE; } else { //not done yet return HTTPD_CGI_MORE; } } }
static void ICACHE_FLASH_ATTR ws_output_write_function(const char * data,size_t len,void *arg){ http_connection *c = (http_connection *)arg; http_nwrite(c,data,len); }