static PT_THREAD(handle_output(struct httpd_state *s)) { char *ptr; PT_BEGIN(&s->outputpt); #if DEBUGLOGIC httpd_strcpy(s->filename,httpd_indexfn); #endif if(!httpd_fs_open(s->filename, &s->file)) { httpd_strcpy(s->filename, httpd_404fn); httpd_fs_open(s->filename, &s->file); PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_404notf)); PT_WAIT_THREAD(&s->outputpt, send_file(s)); } else { PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_200ok)); ptr = strchr(s->filename, ISO_period); if((ptr != NULL && httpd_strncmp(ptr, httpd_shtml, 6) == 0) || httpd_strcmp(s->filename,httpd_indexfn)==0) { PT_INIT(&s->scriptpt); PT_WAIT_THREAD(&s->outputpt, handle_script(s)); } else { PT_WAIT_THREAD(&s->outputpt, send_file(s)); } } PSOCK_CLOSE(&s->sout); PT_END(&s->outputpt); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(handle_output(struct httpd_state *s)) { char *ptr; PT_BEGIN(&s->outputpt); if(!httpd_fs_open(s->filename,&s->file))//打开HTML文件不成功 { httpd_fs_open(http_404_html, &s->file); strcpy(s->filename, http_404_html); PT_WAIT_THREAD(&s->outputpt, send_headers(s,http_header_404)); //发送404失败页面 PT_WAIT_THREAD(&s->outputpt,send_file(s)); }else //打开HTML文件成功 { PT_WAIT_THREAD(&s->outputpt,send_headers(s,http_header_200)); ptr=strchr(s->filename, ISO_period); if(ptr != NULL && strncmp(ptr,http_shtml,6) == 0)//判断文件后缀是否为.SHTML { PT_INIT(&s->scriptpt); PT_WAIT_THREAD(&s->outputpt, handle_script(s)); }else { PT_WAIT_THREAD(&s->outputpt,send_file(s)); } } PSOCK_CLOSE(&s->sout); PT_END(&s->outputpt); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(handle_output(struct httpd_state *s)) { char *ptr; PT_BEGIN(&s->outputpt); if(!httpd_fs_open(s->filename, &s->file)) { strcpy(s->filename, ip64_webserver_http_404_html); httpd_fs_open(s->filename, &s->file); PT_WAIT_THREAD(&s->outputpt, send_headers(s, ip64_webserver_http_header_404)); PT_WAIT_THREAD(&s->outputpt, send_file(s)); } else { PT_WAIT_THREAD(&s->outputpt, send_headers(s, ip64_webserver_http_header_200)); ptr = strrchr(s->filename, ISO_period); if(ptr != NULL && strncmp(ptr, ip64_webserver_http_shtml, 6) == 0) { PT_INIT(&s->scriptpt); PT_WAIT_THREAD(&s->outputpt, handle_script(s)); } else { PT_WAIT_THREAD(&s->outputpt, send_file(s)); } } PSOCK_CLOSE(&s->sout); PT_END(&s->outputpt); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(handle_output(struct httpd_state *s)) { PT_BEGIN(&s->outputpt); char *ptr; if(!httpd_fs_open(s->filename, &s->file)) { httpd_fs_open(http_404_html, &s->file); strcpy_P(s->filename, http_404_html); PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404)); PT_WAIT_THREAD(&s->outputpt, send_file(s)); } else { snprintf(s->tmp_str, sizeof(s->tmp_str) -1, "%d", s->file.len); //snprintf(s->str_tmp, 8, "%d", s->len); PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200)); ptr = strchr(s->filename, ISO_period); if(ptr != NULL && strncmp_P(ptr, http_shtml, 6) == 0) { PT_INIT(&s->scriptpt); PT_WAIT_THREAD(&s->outputpt, handle_script(s)); } else { PT_WAIT_THREAD(&s->outputpt, send_file(s)); } } PSOCK_CLOSE(&s->sout); PT_END(&s->outputpt); }
/* ======================================================================================================================= ======================================================================================================================= */ static PT_THREAD(handle_output (struct httpd_state *s)) { /*~~~~~~~~~*/ char *ptr; /*~~~~~~~~~*/ PT_BEGIN(&s->outputpt); if(!httpd_fs_open(s->filename, &s->file)) { httpd_fs_open(http_404_html, &s->file); strcpy(s->filename, http_404_html); PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404)); PT_WAIT_THREAD(&s->outputpt, send_file(s)); } else { PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200)); ptr = strchr(s->filename, ISO_period); if(ptr != NULL && strncmp(ptr, http_shtml, 6) == 0) { vProcessInput( s->filename ); PT_INIT(&s->scriptpt); PT_WAIT_THREAD(&s->outputpt, handle_script(s)); } else { PT_WAIT_THREAD(&s->outputpt, send_file(s)); } } PSOCK_CLOSE(&s->sout); PT_END(&s->outputpt); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(handle_output(struct httpd_state *s)) { char *ptr; PT_BEGIN(&s->outputpt); if(!httpd_fs_open(s->filename, &s->file)) { httpd_fs_open(http_404_html, &s->file); PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404)); PT_WAIT_THREAD(&s->outputpt, send_file(s)); } else { PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200)); ptr = strchr(s->filename, ISO_period); #if HTTPD_CONF_SCRIPT if(ptr != NULL && strncmp(ptr, http_shtml, 6) == 0) { PT_INIT(&s->scriptpt); PT_WAIT_THREAD(&s->outputpt, handle_script(s)); } else { PT_WAIT_THREAD(&s->outputpt, send_file(s)); } #else /* HTTPD_CONF_SCRIPT */ PT_WAIT_THREAD(&s->outputpt, send_file(s)); #endif /* HTTPD_CONF_SCRIPT */ } PSOCK_CLOSE(&s->sout); PT_END(&s->outputpt); }
/*---------------------------------------------------------------------------*/ static unsigned short generate_file_stats(void *arg) { struct httpd_state *s = (struct httpd_state *)arg; #if WEBSERVER_CONF_LOADTIME static const char httpd_cgi_filestat1[] HTTPD_STRING_ATTR = "<p align=\"right\"><br><br><i>This page has been sent %u times (%1u.%u sec)</i></body></html>"; #else static const char httpd_cgi_filestat1[] HTTPD_STRING_ATTR = "<p align=\"right\"><br><br><i>This page has been sent %u times</i></body></html>"; #endif static const char httpd_cgi_filestat2[] HTTPD_STRING_ATTR = "<tr><td><a href=\"%s\">%s</a></td><td>%d</td>"; static const char httpd_cgi_filestat3[] HTTPD_STRING_ATTR = "%5u"; char tmp[20]; struct httpd_fsdata_file_noconst *f,fram; u16_t i; unsigned short numprinted; /* Transfer arg from whichever flash that contains the html file to RAM */ httpd_fs_cpy(&tmp, s->u.ptr, 20); /* Count for this page, with common page footer */ if (tmp[0]=='.') { #if WEBSERVER_CONF_LOADTIME s->pagetime = clock_time() - s->pagetime; numprinted=httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_filestat1, httpd_fs_open(s->filename, 0), (unsigned int)s->pagetime/CLOCK_SECOND,(unsigned int)s->pagetime%CLOCK_SECOND); #else numprinted=httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_filestat1, httpd_fs_open(s->filename, 0)); #endif /* Count for all files */ /* Note buffer will overflow if there are too many files! */ } else if (tmp[0]=='*') { i=0;numprinted=0; for(f = (struct httpd_fsdata_file_noconst *)httpd_fs_get_root(); f != NULL; f = (struct httpd_fsdata_file_noconst *)fram.next) { /* Get the linked list file entry into RAM from from wherever it is*/ httpd_memcpy(&fram,f,sizeof(fram)); /* Get the file name from whatever memory it is in */ httpd_fs_cpy(&tmp, fram.name, sizeof(tmp)); #if WEBSERVER_CONF_FILESTATS==2 numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_filestat2, tmp, tmp, f->count); #else numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_filestat2, tmp, tmp, httpd_filecount[i]); #endif i++; } /* Count for specified file */ } else { numprinted=httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_filestat3, httpd_fs_open(tmp, 0)); } return numprinted; }
/*---------------------------------------------------------------------------*/ static unsigned short generate_file_stats(void *arg) { static const char httpd_cgi_filestat1[] HTTPD_STRING_ATTR = "<p class=right><br><br><i>This page has been sent %u times</i></div></body></html>"; static const char httpd_cgi_filestat2[] HTTPD_STRING_ATTR = "<tr><td><a href=\"%s\">%s</a></td><td>%d</td>"; static const char httpd_cgi_filestat3[] HTTPD_STRING_ATTR = "%5u"; char tmp[20]; struct httpd_fsdata_file_noconst *f,fram; uint16_t i; unsigned short numprinted; /* Transfer arg from whichever flash that contains the html file to RAM */ httpd_fs_cpy(&tmp, (char *)arg, 20); /* Count for this page, with common page footer */ if (tmp[0]=='.') { numprinted=httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_filestat1, httpd_fs_open(thisfilename, 0)); /* Count for all files */ /* Note buffer will overflow if there are too many files! */ } else if (tmp[0]=='*') { i=0;numprinted=0; for(f = (struct httpd_fsdata_file_noconst *)httpd_fs_get_root(); f != NULL; f = (struct httpd_fsdata_file_noconst *)fram.next) { /* Get the linked list file entry into RAM from from wherever it is*/ httpd_memcpy(&fram,f,sizeof(fram)); /* Get the file name from whatever memory it is in */ httpd_fs_cpy(&tmp, fram.name, sizeof(tmp)); #if HTTPD_FS_STATISTICS==1 numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_filestat2, tmp, tmp, f->count); #elif HTTPD_FS_STATISTICS==2 numprinted+=httpd_snprintf((char *)uip_appdata+numprinted, uip_mss()-numprinted, httpd_cgi_filestat2, tmp, tmp, httpd_filecount[i]); #endif i++; } /* Count for specified file */ } else { numprinted=httpd_snprintf((char *)uip_appdata, uip_mss(), httpd_cgi_filestat3, httpd_fs_open(tmp, 0)); } #if DEBUGLOGIC return 0; #endif return numprinted; }
/*---------------------------------------------------------------------------*/ static PT_THREAD(handle_script(struct httpd_state *s)) { char *ptr; PT_BEGIN(&s->scriptpt); while(s->file.len > 0) { /* Check if we should start executing a script. */ if(*s->file.data == ISO_percent && *(s->file.data + 1) == ISO_bang) { s->scriptptr = s->file.data + 3; s->scriptlen = s->file.len - 3; if(*(s->scriptptr - 1) == ISO_colon) { httpd_fs_open(s->scriptptr + 1, &s->file); PT_WAIT_THREAD(&s->scriptpt, send_file(s)); } else { // Httpd_cgi is called here PT_WAIT_THREAD(&s->scriptpt, httpd_cgi(s->scriptptr)(s, s->scriptptr)); } next_scriptstate(s); /* The script is over, so we reset the pointers and continue sending the rest of the file. */ s->file.data = s->scriptptr; s->file.len = s->scriptlen; } else { /* See if we find the start of script marker in the block of HTML to be sent. */ if(s->file.len > uip_mss()) { s->len = uip_mss(); } else { s->len = s->file.len; } if(*s->file.data == ISO_percent) { ptr = strchr(s->file.data + 1, ISO_percent); } else { ptr = strchr(s->file.data, ISO_percent); } if(ptr != NULL && ptr != s->file.data) { s->len = (int)(ptr - s->file.data); if(s->len >= uip_mss()) { s->len = uip_mss(); } } PT_WAIT_THREAD(&s->scriptpt, send_part_of_file(s)); s->file.data += s->len; s->file.len -= s->len; } } PT_END(&s->scriptpt); }
static PT_THREAD(handle_output(struct httpd_state *s)) { char *ptr; PT_BEGIN(&s->outputpt); #if DEBUGLOGIC httpd_strcpy(s->filename,httpd_indexfn); #endif if(!httpd_fs_open(s->filename, &s->file)) { httpd_strcpy(s->filename, httpd_404fn); httpd_fs_open(s->filename, &s->file); PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_404notf)); PT_WAIT_THREAD(&s->outputpt, send_file(s)); } else { PRINTF("s->state: %d\r\n", s->state); if(s->state == STATE_OUTPUT) { PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_200ok)); ptr = strchr(s->filename, ISO_period); if((ptr != NULL && httpd_strncmp(ptr, httpd_shtml, 6) == 0) || httpd_strcmp(s->filename,httpd_indexfn)==0) { PT_INIT(&s->scriptpt); PT_WAIT_THREAD(&s->outputpt, handle_script(s)); } else { PT_WAIT_THREAD(&s->outputpt, send_file(s)); } } else { // state == STATE_OUTPUT_ERROR if(s->error_number == 413) { // Send the error message file instead. httpd_strcpy(s->filename, httpd_413fn); httpd_fs_open(s->filename, &s->file); PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_413error)); PT_WAIT_THREAD(&s->outputpt, send_file(s)); } // other error messages can be added here. } } PSOCK_CLOSE(&s->sout); PT_END(&s->outputpt); }
/* next_sciptstate: * * Reads one line of script and decides what to do next. */ static void next_scriptstate(void) { struct httpd_fs_file fsfile; u8_t i; again: switch(hs->script[0]) { case ISO_t: /* Send a text string. */ hs->state = HTTP_TEXT; hs->dataptr = &hs->script[2]; /* Calculate length of string. */ for(i = 0; hs->dataptr[i] != ISO_nl; ++i); hs->count = i; break; case ISO_c: /* Call a function. */ hs->state = HTTP_FUNC; hs->dataptr = NULL; hs->count = 0; uip_reset_acked(); break; case ISO_i: /* Include a file. */ hs->state = HTTP_FILE; if(!httpd_fs_open(&hs->script[2], &fsfile)) { uip_abort(); dealloc_state(hs); } hs->dataptr = fsfile.data; hs->count = fsfile.len; break; case ISO_hash: /* Comment line. */ next_scriptline(); goto again; break; case ISO_period: /* End of script. */ hs->state = HTTP_END; uip_close(); dealloc_state(hs); break; default: uip_abort(); dealloc_state(hs); break; } }
/*---------------------------------------------------------------------------*/ static PT_THREAD(handle_output(struct httpd_state *s)) { char *ptr; PT_BEGIN(&s->outputpt); if(!httpd_fs_open(s->filename, &s->file)) { httpd_fs_open(http_404_html, &s->file); strcpy(s->filename, http_404_html); PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404)); PT_WAIT_THREAD(&s->outputpt, send_file(s)); } else { PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200)); ptr = strchr(s->filename, ISO_period); if(ptr != NULL && (strncmp(ptr, http_shtml, 6) == 0 || strncmp(ptr, ".bin", 4) == 0)) { PT_INIT(&s->scriptpt); PT_WAIT_THREAD(&s->outputpt, handle_script(s)); } else { PT_WAIT_THREAD(&s->outputpt, send_file(s)); } } if (s->emumode) { // Just switched to emulator mode! Fix the state s->state = STATE_WAITING; } else { PSOCK_CLOSE(&s->sout); } PT_END(&s->outputpt); }
static PT_THREAD(handle_script(struct httpd_state *s)) { /* Note script includes will attach a leading : to the filename and a trailing zero */ static char scriptname[MAX_SCRIPT_NAME_LENGTH+1],*pptr; static uint16_t filelength; PT_BEGIN(&s->scriptpt); filelength=s->file.len; while(s->file.len > 0) { /* Sanity check */ if (s->file.len > filelength) break; /* Check if we should start executing a script, flagged by %! */ if(httpd_fs_getchar(s->file.data) == ISO_percent && httpd_fs_getchar(s->file.data + 1) == ISO_bang) { /* Extract name, if starts with colon include file else call cgi */ s->scriptptr=get_scriptname(scriptname,s->file.data+2); s->scriptlen=s->file.len-(s->scriptptr-s->file.data); PRINTF("httpd: Handle script named %s\n",scriptname); if(scriptname[0] == ISO_colon) { if (httpd_fs_open(&scriptname[1], &s->file)) { PT_WAIT_THREAD(&s->scriptpt, send_file(s)); } } else { PT_WAIT_THREAD(&s->scriptpt,httpd_cgi(scriptname)(s, s->scriptptr)); } next_scriptstate(s); /* Reset the pointers and continue sending the current file. */ s->file.data = s->scriptptr; s->file.len = s->scriptlen; } else { /* Send file up to the next potential script */ if(s->file.len > uip_mss()) { s->len = uip_mss(); } else { s->len = s->file.len; } if(httpd_fs_getchar(s->file.data) == ISO_percent) { pptr = (char *) httpd_fs_strchr(s->file.data + 1, ISO_percent); } else { pptr = (char *) httpd_fs_strchr(s->file.data, ISO_percent); } if(pptr != NULL && pptr != s->file.data) { s->len = (int)(pptr - s->file.data); if(s->len >= uip_mss()) { s->len = uip_mss(); } } PRINTF("httpd: Sending %u bytes from 0x%04x\n",s->file.len,(unsigned int)s->file.data); PT_WAIT_THREAD(&s->scriptpt, send_part_of_file(s)); s->file.data += s->len; s->file.len -= s->len; } } PT_END(&s->scriptpt); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr)) { char *ptr; PSOCK_BEGIN(&s->sout); PSOCK_GENERATOR_SEND(&s->sout, generate_status, (char *) statushdr); ptr = strrchr(s->filename, ISO_period); if (httpd_strncmp("4", statushdr, 1) == 0) { //404 PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_htm); } else if (ptr == NULL) { #if WEBSERVER_CONF_BIN PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_bin); #else PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_htm); #endif } else { ptr++; #if WEBSERVER_CONF_INCLUDE || WEBSERVER_CONF_CGI if (httpd_strncmp(ptr, &httpd_mime_htm[5], 3) == 0 || httpd_strncmp(ptr, &httpd_str_shtml[1], 4) == 0) { #else if (httpd_strncmp(ptr, &httpd_mime_htm[5], 3) == 0) { #endif PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_htm); #if WEBSEVER_CONF_CSS } else if (httpd_strcmp(ptr, &httpd_mime_css[5]) == 0) { PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_css); #endif #if WEBSERVER_CONF_PNG } else if (httpd_strcmp(ptr, &httpd_mime_png[6]) == 0) { PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_png); #endif #if WEBSERVER_CONF_GIF } else if (httpd_strcmp(ptr, &httpd_mime_gif[6]) == 0) { PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_gif); #endif #if WEBSERVER_CONF_JPG } else if (httpd_strcmp(ptr, httpd_mime_jpg) == 0) { PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_jpg); #endif #if WEBSERVER_CONF_TXT } else { PSOCK_GENERATOR_SEND(&s->sout, generate_header, &httpd_mime_txt); #endif } } PSOCK_END(&s->sout); } /*---------------------------------------------------------------------------*/ static PT_THREAD(handle_output(struct httpd_state *s)) { char *ptr; PT_BEGIN(&s->outputpt); #if DEBUGLOGIC httpd_strcpy(s->filename, httpd_str_indexfn); #endif s->fd = httpd_fs_open(s->filename, HTTPD_FS_READ); if (s->fd == -1) { // Opening file failed. /* TODO: try index.htm ? */ #if WEBSERVER_CONF_INCLUDE || WEBSERVER_CONF_CGI /* If index.html not found try index.htm */ if (httpd_strcmp(s->filename, httpd_str_indexfn) == 0) { httpd_strcpy(s->filename, httpd_str_indexfn3); } s->fd = httpd_fs_open(s->filename, HTTPD_FS_READ); if (s->fd != -1) { goto sendfile; } /* If index.html not found try index.shtml */ if (httpd_strcmp(s->filename, httpd_str_indexfn) == 0) { httpd_strcpy(s->filename, httpd_str_indexsfn); } s->fd = httpd_fs_open(s->filename, HTTPD_FS_READ); if (s->fd == -1) { PRINTD("Opening %s failed\n", s->filename); goto psock_close; } else { goto sendfile; } #endif /* If nothing was found, send 404 page */ httpd_strcpy(s->filename, httpd_str_404fn); s->fd = httpd_fs_open(s->filename, HTTPD_FS_READ); PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_str_404notf)); PT_WAIT_THREAD(&s->outputpt, send_file(s)); } else { // Opening file succeeded. sendfile: PT_WAIT_THREAD(&s->outputpt, send_headers(s, httpd_str_200ok)); #if WEBSERVER_CONF_INCLUDE || WEBSERVER_CONF_CGI /* If filename ends with .shtml, scan file for script includes or cgi */ ptr = strchr(s->filename, ISO_period); if ((ptr != NULL && httpd_strncmp(ptr, httpd_str_shtml, 6) == 0) || httpd_strcmp(s->filename, httpd_str_indexfn) == 0) { PT_INIT(&s->scriptpt); PT_WAIT_THREAD(&s->outputpt, handle_scripts(s)); } else { #else if (1) { #endif PT_WAIT_THREAD(&s->outputpt, send_file(s)); httpd_fs_close(s->fd); } } psock_close: PSOCK_CLOSE(<y & s->sout); PT_END(&s->outputpt); } /*---------------------------------------------------------------------------*/ #if WEBSERVER_CONF_PASSQUERY char httpd_query[WEBSERVER_CONF_PASSQUERY]; #endif static PT_THREAD(handle_input(struct httpd_state *s)) { PSOCK_BEGIN(&s->sin); PSOCK_READTO(&s->sin, ISO_space); if (httpd_strncmp(s->inputbuf, httpd_str_get, 4) != 0) { PSOCK_CLOSE_EXIT(&s->sin); } PSOCK_READTO(&s->sin, ISO_space); if (s->inputbuf[0] != ISO_slash) { PSOCK_CLOSE_EXIT(&s->sin); } if (s->inputbuf[1] == ISO_space) { httpd_strcpy(s->filename, httpd_str_indexfn); } else { uint8_t i; for (i = 0; i<sizeof (s->filename) + 1; i++) { if (i >= (PSOCK_DATALEN(&s->sin) - 1)) break; if (s->inputbuf[i] == ISO_space) break; #if WEBSERVER_CONF_PASSQUERY /* Query string is left in the httpd_query buffer until zeroed by the application! */ if (s->inputbuf[i] == ISO_qmark) { strncpy(httpd_query, &s->inputbuf[i + 1], sizeof (httpd_query)); break; } #endif s->filename[i] = s->inputbuf[i]; } s->filename[i] = 0; } #if WEBSERVER_CONF_LOG webserver_log_file(&uip_conn->ripaddr, s->filename); // webserver_log(httpd_query); #endif #if WEBSERVER_CONF_LOADTIME s->pagetime = clock_time(); #endif s->state = STATE_OUTPUT; while (1) { PSOCK_READTO(&s->sin, ISO_nl); #if WEBSERVER_CONF_LOG && WEBSERVER_CONF_REFERER if (httpd_strncmp(s->inputbuf, httpd_str_ref, 8) == 0) { s->inputbuf[PSOCK_DATALEN(&s->sin) - 2] = 0; petsciiconv_topetscii(s->inputbuf, PSOCK_DATALEN(&s->sin) - 2); webserver_log(s->inputbuf); } #endif } PSOCK_END(&s->sin); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(handle_script(struct httpd_state *s)) { PGM_P ptr; PT_BEGIN(&s->scriptpt); while(s->file.len > 0) { /* Check if we should start executing a script. */ if(pgm_read_byte_near(s->file.data) == ISO_percent && pgm_read_byte_near(s->file.data + 1) == ISO_bang) { s->scriptptr = s->file.data + 3; s->scriptlen = s->file.len - 3; if(pgm_read_byte_near(s->scriptptr - 1) == ISO_colon) { strncpy_P(s->tmp_str, s->scriptptr + 1, sizeof(s->tmp_str) -1); if(httpd_fs_open(s->tmp_str, &s->file) || httpd_fs_open(s->tmp_str + 1 , &s->file)) { PT_WAIT_THREAD(&s->scriptpt, send_file(s)); } else { // could not open the file. } } else { strncpy_P(s->tmp_str, s->scriptptr, sizeof(s->tmp_str) -1); ((s->tmp_str)[sizeof(s->tmp_str) - 1]) = '\0'; PT_WAIT_THREAD(&s->scriptpt, httpd_cgi(s->tmp_str)(s, s->tmp_str)); } next_scriptstate(s); /* The script is over, so we reset the pointers and continue sending the rest of the file. */ s->file.data = s->scriptptr; s->file.len = s->scriptlen; } else { /* See if we find the start of script marker in the block of HTML to be sent. */ if(s->file.len > uip_mss()) { s->len = uip_mss(); } else { s->len = s->file.len; } if(pgm_read_byte_near(s->file.data) == ISO_percent) { ptr = strchr_P(s->file.data + 1, ISO_percent); } else { ptr = strchr_P(s->file.data, ISO_percent); } if(ptr != NULL && ptr != s->file.data) { s->len = (int)(ptr - s->file.data); if(s->len >= uip_mss()) { s->len = uip_mss(); } } PT_WAIT_THREAD(&s->scriptpt, send_part_of_file(s)); s->file.data += s->len; s->file.len -= s->len; } } PT_END(&s->scriptpt); /* PGM_P ptr; PT_BEGIN(&s->scriptpt); while(s->file.len > 0) { // Check if we should start executing a script. if( (pgm_read_byte(s->file.data) == ISO_percent) && (pgm_read_byte(s->file.data + 1) == ISO_bang)) { s->scriptptr = s->file.data + 3; s->scriptlen = s->file.len - 3; if(pgm_read_byte(s->scriptptr - 1) == ISO_colon) { strncpy_P(s->tmp_str, s->scriptptr + 1, sizeof(s->tmp_str) -1); if (httpd_fs_open(s->tmp_str, &s->file)) { PT_WAIT_THREAD(&s->scriptpt, send_file(s)); } } else { PT_WAIT_THREAD(&s->scriptpt, httpd_cgi(s->scriptptr)(s, s->scriptptr)); } next_scriptstate(s); // The script is over, so we reset the pointers and continue // sending the rest of the file. s->file.data = s->scriptptr; s->file.len = s->scriptlen; } else { // See if we find the start of script marker in the block of HTML // to be sent. if(s->file.len > uip_mss()) { s->len = uip_mss(); } else { s->len = s->file.len; } if(pgm_read_byte(s->file.data) == ISO_percent) { ptr = strchr_P(s->file.data + 1, ISO_percent); } else { ptr = strchr_P(s->file.data, ISO_percent); } if(ptr != NULL && ptr != s->file.data) { s->len = (int)(ptr - s->file.data); if(s->len >= uip_mss()) { s->len = uip_mss(); } } PT_WAIT_THREAD(&s->scriptpt, send_part_of_file(s)); s->file.data += s->len; s->file.len -= s->len; } } PT_END(&s->scriptpt); */ }
/*-----------------------------------------------------------------------------------*/ DISPATCHER_UIPCALL(httpd_appcall, state) { struct httpd_fs_file fsfile; u8_t i; DISPATCHER_UIPCALL_ARG(state); hs = (struct httpd_state *)(state); /* We use the uip_ test functions to deduce why we were called. If uip_connected() is non-zero, we were called because a remote host has connected to us. If uip_newdata() is non-zero, we were called because the remote host has sent us new data, and if uip_acked() is non-zero, the remote host has acknowledged the data we previously sent to it. */ if(uip_connected()) { /* Since we've just been connected, the state pointer should be NULL and we need to allocate a new state object. If we have run out of memory for state objects, we'll have to abort the connection and return. */ if(hs == NULL) { hs = alloc_state(); if(hs == NULL) { uip_close(); return; } dispatcher_markconn(uip_conn, (void *)hs); } /* Since we have just been connected with the remote host, we reset the state for this connection. The ->count variable contains the amount of data that is yet to be sent to the remote host, and the ->state is set to HTTP_NOGET to signal that we haven't received any HTTP GET request for this connection yet. */ hs->state = HTTP_NOGET; hs->count = 0; hs->poll = 0; } else if(uip_closed() || uip_aborted()) { if(hs != NULL) { dealloc_state(hs); } return; } else if(uip_poll()) { /* If we are polled ten times, we abort the connection. This is because we don't want connections lingering indefinately in the system. */ if(hs != NULL) { if(hs->state == HTTP_DEALLOCATED) { uip_abort(); } else if(hs->poll++ >= 100) { uip_abort(); dealloc_state(hs); } } return; } if(uip_newdata() && hs->state == HTTP_NOGET) { hs->poll = 0; /* This is the first data we receive, and it should contain a GET. */ /* Check for GET. */ if(uip_appdata[0] != ISO_G || uip_appdata[1] != ISO_E || uip_appdata[2] != ISO_T || uip_appdata[3] != ISO_space) { /* If it isn't a GET, we abort the connection. */ uip_abort(); dealloc_state(hs); return; } beep(); /* Find the file we are looking for. */ for(i = 4; i < 40; ++i) { if(uip_appdata[i] == ISO_space || uip_appdata[i] == ISO_cr || uip_appdata[i] == ISO_nl) { uip_appdata[i] = 0; break; } } PRINT("request for file "); PRINTLN(&uip_appdata[4]); webserver_log_file(uip_conn->ripaddr, &uip_appdata[4]); /* Check for a request for "/". */ if(uip_appdata[4] == ISO_slash && uip_appdata[5] == 0) { httpd_fs_open(file_index_html.name, &fsfile); } else { if(!httpd_fs_open((const char *)&uip_appdata[4], &fsfile)) { PRINTLN("couldn't open file"); httpd_fs_open(file_404_html.name, &fsfile); } } if(uip_appdata[4] == ISO_slash && uip_appdata[5] == ISO_c && uip_appdata[6] == ISO_g && uip_appdata[7] == ISO_i && uip_appdata[8] == ISO_slash) { /* If the request is for a file that starts with "/cgi/", we prepare for invoking a script. */ hs->script = fsfile.data; next_scriptstate(); } else { hs->script = NULL; /* The web server is now no longer in the HTTP_NOGET state, but in the HTTP_FILE state since is has now got the GET from the client and will start transmitting the file. */ hs->state = HTTP_FILE; /* Point the file pointers in the connection state to point to the first byte of the file. */ hs->dataptr = fsfile.data; hs->count = fsfile.len; } } if(hs->state != HTTP_FUNC) { /* Check if the client (remote end) has acknowledged any data that we've previously sent. If so, we move the file pointer further into the file and send back more data. If we are out of data to send, we close the connection. */ if(uip_acked()) { hs->poll = 0; if(hs->count >= uip_mss()) { hs->count -= uip_mss(); hs->dataptr += uip_mss(); } else { hs->count = 0; } if(hs->count == 0) { if(hs->script != NULL) { next_scriptline(); next_scriptstate(); } else { uip_close(); dealloc_state(hs); } } } } if(hs->state == HTTP_FUNC) { /* Call the CGI function. */ #if 1 if(httpd_cgitab[hs->script[2] - ISO_a]()) { /* If the function returns non-zero, we jump to the next line in the script. */ next_scriptline(); next_scriptstate(); } #endif } if(hs->state != HTTP_FUNC && !uip_poll()) { hs->poll = 0; /* Send a piece of data, but not more than the MSS of the connection. */ uip_send(hs->dataptr, hs->count > uip_mss()? uip_mss(): hs->count); } /* Finally, return to uIP. Our outgoing packet will soon be on its way... */ }
/*---------------------------------------------------------------------------*/ static PT_THREAD(handle_output(struct httpd_state *s)) { char *ptr; PT_BEGIN(&s->outputpt); if(!httpd_fs_open(s->filename, &s->file)) { //printf(" HTTP file %s tidak ada\n", s->filename); httpd_fs_open(http_404_html, &s->file); strcpy(s->filename, http_404_html); PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404)); PT_WAIT_THREAD(&s->outputpt, send_file(s)); } else { //* #ifdef PAKAI_HTTP if (strncmp(s->filename, "/about", 6) == 0) { //printf(" Buat file about\r\n"); buat_file_about(); s->file.len = strlen(tot_buf); portENTER_CRITICAL(); s->file.data = tot_buf; portEXIT_CRITICAL(); } else if (strncmp(s->filename, "/setting", 8) == 0) { //printf(" Buat file setting: %s\r\n", s->filename); if (strncmp(s->filename,"/setting.html?u=1",17)==0) { ganti_setting(s->filename); buat_file_setting(1, s->filename); } else if (strncmp(s->filename,"/setting.html?smb=1",19)==0) { buat_file_setting(2, s->filename); } else if (strncmp(s->filename,"/setting.html?smb=3",19)==0) { buat_file_setting(3, s->filename); printf("buat_file_setting: OK\r\n"); } else if (strncmp(s->filename,"/setting.html?smb=4",19)==0) { buat_file_setting(4, s->filename); #ifdef TES_GET_WEB } else if (strncmp(s->filename,"/setting.html?smb=5",19)==0) { buat_file_setting(5, s->filename); #endif } else { buat_file_setting(0,""); } s->file.len = strlen(tot_buf); printf("Kirim file ke client. len: %d\n",s->file.len); portENTER_CRITICAL(); s->file.data = tot_buf; portEXIT_CRITICAL(); } #ifdef PAKAI_PM else if (strncmp(s->filename, "/index.html?sbr=1", 17) == 0) { buat_file_index(1, s->filename); s->file.len = strlen(tot_buf); portENTER_CRITICAL(); s->file.data = tot_buf; portEXIT_CRITICAL(); } #endif else { //printf(" Buat file index\r\n"); buat_file_index(0, ""); s->file.len = strlen(tot_buf); portENTER_CRITICAL(); s->file.data = tot_buf; portEXIT_CRITICAL(); } #endif PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200)); ptr = strchr(s->filename, ISO_period); if(ptr != NULL && strncmp(ptr, http_shtml, 6) == 0) { //printf(" script !\n"); PT_INIT(&s->scriptpt); PT_WAIT_THREAD(&s->outputpt, handle_script(s)); } else { //printf(" biasa !\n"); PT_WAIT_THREAD(&s->outputpt, send_file(s)); } } PSOCK_CLOSE(&s->sout); PT_END(&s->outputpt); }
/** * Parses shtml document for possible script includes. * Accepts includes of the form '%! scriptname'. * * If scriptname starts with ':' it is assumed to be an SSI, * otherwise it will be handled as CGI call. */ static PT_THREAD(handle_scripts(struct httpd_state *s)) { /* Note script includes will attach a leading : to the filename and a trailing zero */ static char scriptname[MAX_SCRIPT_NAME_LENGTH + 1], *pptr; // static uint16_t filelength; static int done, eof, eoc; PT_BEGIN(&s->scriptpt); eoc = eof = done = 0; /* Init cache (null-terminated)*/ load_cache(s); while (!done) { /* Assure we can read at least a '%!' sequence. */ // if (cache_size() < 2) { // reload_cache(s); // } /* Check if we should start executing a script, flagged by %! */ if (file_cache_ptr[0] == ISO_percent && file_cache_ptr[1] == ISO_bang) { /* Test if a whole line is in cache, otherwise reload cache. */ if (strchr(&file_cache_ptr[2], ISO_nl) == NULL) { reload_cache(s); } /* Extract name, if starts with colon include file else call cgi */ s->scriptptr = get_scriptname(scriptname, &file_cache_ptr[2]); // s->scriptlen = files[s->fd].len - (s->scriptptr - file_cache_ptr[0]); PRINTD("httpd: Handle script named %s\n", scriptname); /* Include scripts prefixed with ':' are SSI else CGI scripts */ if (scriptname[0] == ISO_colon) { #if WEBSERVER_CONF_INCLUDE PRINTD("IS SSI\n"); s->scriptfd = httpd_fs_open(&scriptname[1], HTTPD_FS_READ); /* Send script if open succeeded. */ if (s->scriptfd != -1) { s->sendfd = s->scriptfd; PT_WAIT_THREAD(&s->scriptpt, send_file(s)); } else { PRINTD("failed opening %s\n", scriptname); } httpd_fs_close(s->scriptfd); /*TODO dont print anything if file not found */ #endif /* Execute unprefixed scripts. */ } else { PRINTD("IS CGI\n"); #if WEBSERVER_CONF_CGI PT_WAIT_THREAD(&s->scriptpt, httpd_cgi(scriptname)(s, s->scriptptr)); #endif } skip_scriptline(s); file_cache_ptr = s->scriptptr; if (cache_size() < 2) { reload_cache(s); } if (cache_size() == 0) { done = 1; } } else { // no script /* get position of next percent character */ if (file_cache_ptr[0] == ISO_percent) { pptr = (char *) strchr(&file_cache_ptr[1], ISO_percent); } else { pptr = (char *) strchr(&file_cache_ptr[0], ISO_percent); } /* calc new length to send */ if (pptr == NULL) { /* no further percent sign found in cache. */ /* Send to end of cache */ s->sendlen = cache_len - (&file_cache_ptr[0] - &file_cache_array[0]); eoc = 1;// inidcates thate we need new cache } else { s->sendlen = (int) ((int) pptr - (int) &file_cache_ptr[0]); } if (s->sendlen > 0) { PRINTD("httpd: Sending %u bytes from 0x%04x\n", s->sendlen, (unsigned int) pptr); s->sendfd = s->fd; PT_WAIT_THREAD(&s->scriptpt, send_part_of_cache(s)); } /* Reload cache if it was marked as empty. */ if (eoc) { reload_cache(s); } /* If (reloaded) cache empty, stop sending */ if (cache_size() == 0) { done = 1; } } } PT_END(&s->scriptpt); }