/*---------------------------------------------------------------------------*/ 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); }
/* ll_shell_ftp server uses this to send a file from wrt54g to host, when * host requests it. * * the message contains the name of the file to send. * * find file /tmp/(file to send) on wrt54g and send that file. * * set state to "state_sending_file". */ static void start_sending_file(message_t *msg) { unsigned char buf[512]; char fname[PATH_MAX]; unsigned char *p; p = &msg->msg_body[msg->msg_body_len - 1]; while (p > &msg->msg_body[0] && *(p-1) != '/') { p--; } snprintf(fname, PATH_MAX, "/tmp/%s", p); fd = open(fname, O_RDONLY); if (fd == -1) { perror("send_file: open failed"); fprintf(stderr, "file that could not be opened: %s\n", fname); send_message(buf, 1, recv_err_stop_msg); return; } seq = 1; state = state_sending_file; send_part_of_file(fd); }
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(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); */ }