static PT_THREAD(handle_output(struct httpd_state *s)) { PT_BEGIN(&s->outputpt); s->script = NULL; s->script = httpd_simple_get_script(&s->filename[1]); if(s->script == NULL) { strncpy(s->filename, "/notfound.html", sizeof(s->filename)); PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404)); PT_WAIT_THREAD(&s->outputpt, send_string(s, NOT_FOUND)); uip_close(); webserver_log_file(&uip_conn->ripaddr, "404 - not found"); PT_EXIT(&s->outputpt); } else { PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200)); PT_WAIT_THREAD(&s->outputpt, s->script(s)); } s->script = NULL; 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 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 int hello_world_appcall(void) { struct tcp_hello_appstate *s = &(uip_conn->appstate.tcp_hello); if(uip_connected()) { PSOCK_INIT(&s->p, s->inputbuffer, sizeof(s->inputbuffer)); } PSOCK_BEGIN(&s->p); PSOCK_SEND_STR(&s->p, "Hello. What is your name?\n"); PSOCK_READTO(&s->p, '\n'); strncpy(s->name, s->inputbuffer, sizeof(s->name)); s->name[strlen(s->name)-2] = 0; PSOCK_SEND_STR(&s->p, "Hello "); PSOCK_SEND_STR(&s->p, s->name); PSOCK_SEND_STR(&s->p, " !\n"); sprintf(s->name, "r %d/%d", nrf_link_get_rx_packets(), nrf_link_get_rx_dropped()); PSOCK_SEND_STR(&s->p, s->name); PSOCK_SEND_STR(&s->p, " "); sprintf(s->name, "t %d/%d", nrf_link_get_tx_packets(), nrf_link_get_tx_dropped()); PSOCK_SEND_STR(&s->p, s->name); PSOCK_SEND_STR(&s->p, "\n\r"); PSOCK_CLOSE(&s->p); PSOCK_END(&s->p); }
// This function is going to use uIP's protosockets to handle the connection. // This means it must return int, because of the way the protosockets work. // In a nutshell, when a PSOCK_* macro needs to wait for something, it will // return from handle_connection so that other work can take place. When the // event is triggered, uip_callback() will call this function again and the // switch() statement (see below) will take care of resuming execution where // it left off. It *looks* like this function runs from start to finish, but // that's just an illusion to make it easier to code :-) int UIPClient::handle_connection(uip_tcp_appstate_t *s) { // All protosockets must start with this macro. Its internal implementation // is that of a switch() statement, so all code between PSOCK_BEGIN and // PSOCK_END is actually inside a switch block. (This means for example, // that you can't declare variables in the middle of it!) struct psock *p = &s->p; uip_userdata_t *u = (uip_userdata_t *) s->user; PSOCK_BEGIN(p); if (uip_newdata() && readyToReceive(s)) { PSOCK_READBUF_LEN(p, 0); //TODO check what happens when there's more new data incoming than space left in UIPClients buffer (most likely this is just discarded, isn't it?) dataReceived(s); } if (readyToSend(s)) { PSOCK_SEND(p, u->out_buffer, u->out_len); dataSent(s); } if (isClosed(s)) { // Disconnect. PSOCK_CLOSE(p); } // All protosockets must end with this macro. It closes the switch(). PSOCK_END(p); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(handle_output(struct httpd_state *s)) { PT_BEGIN(&s->outputpt); petsciiconv_topetscii(s->filename, sizeof(s->filename)); s->fd = cfs_open(s->filename, CFS_READ); petsciiconv_toascii(s->filename, sizeof(s->filename)); if(s->fd < 0) { strcpy(s->filename, "notfound.html"); s->fd = cfs_open(s->filename, CFS_READ); petsciiconv_toascii(s->filename, sizeof(s->filename)); if(s->fd < 0) { PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404)); PT_WAIT_THREAD(&s->outputpt, send_string(s, "not found")); uip_close(); webserver_log_file(&uip_conn->ripaddr, "404 (no notfound.html)"); PT_EXIT(&s->outputpt); } PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404)); webserver_log_file(&uip_conn->ripaddr, "404 - notfound.html"); } else { PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200)); } PT_WAIT_THREAD(&s->outputpt, send_file(s)); cfs_close(s->fd); s->fd = -1; PSOCK_CLOSE(&s->sout); PT_END(&s->outputpt); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(handle_output(struct httpd_state *s)) { PT_BEGIN(&s->outputpt); s->fd = cfs_open(s->filename, CFS_READ); if(s->fd < 0) { s->fd = cfs_open("404.html", CFS_READ); if(s->fd < 0) { uip_abort(); PT_EXIT(&s->outputpt); } PT_WAIT_THREAD(&s->outputpt, send_headers(s, "HTTP/1.0 404 Not found\r\n")); PT_WAIT_THREAD(&s->outputpt, send_file(s)); } else { PT_WAIT_THREAD(&s->outputpt, send_headers(s, "HTTP/1.0 200 OK\r\n")); PT_WAIT_THREAD(&s->outputpt, send_file(s)); cfs_close(s->fd); } 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)) { PT_BEGIN(&s->outputpt); s->script = httpd_cgi(&s->filename[1]); if(!s->script) { httpd_cgi_command_t *cmd = httpd_cgi_command(&s->filename[1]); if(cmd) { s->script = cmd->function(s); } } if(s->script) { if((s->script->flags & HTTPD_CUSTOM_HEADER) == 0) { PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200)); } if((s->script->flags & HTTPD_CUSTOM_TOP) == 0) { PT_WAIT_THREAD(&s->outputpt, generate_top(s)); } PT_WAIT_THREAD(&s->outputpt, s->script->function(s)); if((s->script->flags & HTTPD_CUSTOM_BOTTOM) == 0) { PT_WAIT_THREAD(&s->outputpt, generate_bottom(s)); } #if CONTIKI_TARGET_NATIVE } else if (httpd_is_file(s->filename)){ PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200)); PT_WAIT_THREAD(&s->outputpt, send_file(s)); #endif } else { LOG6LBR_6ADDR(WARN, &uip_conn->ripaddr, "File '%s' not found, from ", s->filename); PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404)); PT_WAIT_THREAD(&s->outputpt, generate_404(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); }
/* * The definition of the process. */ PROCESS_THREAD(sensor_psock_server_process, ev, data) { PROCESS_BEGIN(); tcp_listen(UIP_HTONS(1010)); while(1) { PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event); if(uip_connected()) { PSOCK_INIT(&ps, buffer, sizeof(buffer)); while(!(uip_aborted() || uip_closed() || uip_timedout())) { PROCESS_WAIT_EVENT_UNTIL(ev == tcpip_event); handle_connection(&ps); } PSOCK_CLOSE(&ps); } } PSOCK_CLOSE_EXIT(&ps); PROCESS_END(); }
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)) { 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 int handle_connection(struct socket_app_state *s) { PSOCK_BEGIN(&s->p); PSOCK_SEND_STR(&s->p, socket_response); PSOCK_CLOSE(&s->p); PSOCK_END(&s->p); }
/* * Simple protothread to send a 421 (service unavailable) * when either the USB drive isn't attached or there aren't * any free dtpd_state entries. */ static PT_THREAD(send_421_response( ftpd_appstate_t *s )) { PSOCK_BEGIN(&s->ps); PSOCK_SEND_STR(&s->ps, Resp421); PSOCK_CLOSE(&s->ps); PSOCK_END(&s->ps); }
/* * A protosocket always requires a protothread. The protothread * contains the code that uses the protosocket. We define the * protothread here. */ static PT_THREAD(handle_connection(struct psock *p)) { /* * A protosocket's protothread must start with a PSOCK_BEGIN(), with * the protosocket as argument. * * Remember that the same rules as for protothreads apply: do NOT * use local variables unless you are very sure what you are doing! * Local (stack) variables are not preserved when the protothread * blocks. */ PSOCK_BEGIN(p); /* * We start by sending out a welcoming message. The message is sent * using the PSOCK_SEND_STR() function that sends a null-terminated * string. */ PSOCK_SEND_STR(p, "Welcome, please type something and press return.\n"); /* * Next, we use the PSOCK_READTO() function to read incoming data * from the TCP connection until we get a newline character. The * number of bytes that we actually keep is dependant of the length * of the input buffer that we use. Since we only have a 10 byte * buffer here (the buffer[] array), we can only remember the first * 10 bytes received. The rest of the line up to the newline simply * is discarded. */ PSOCK_READTO(p, '\n'); /* * And we send back the contents of the buffer. The PSOCK_DATALEN() * function provides us with the length of the data that we've * received. Note that this length will not be longer than the input * buffer we're using. */ PSOCK_SEND_STR(p, "Got the following data: "); PSOCK_SEND(p, buffer, PSOCK_DATALEN(p)); PSOCK_SEND_STR(p, "Good bye!\r\n"); /* * We close the protosocket. */ PSOCK_CLOSE(p); /* * And end the protosocket's protothread. */ PSOCK_END(p); }
static int handle_connection(struct webserver_state *s) { PSOCK_BEGIN(&s->p); /* the incoming GET request will have the following format: * GET / HTTP/1.1 .... * we have to parse this string to determine the resource being requested * if the requested resource is not the root webpage ('/') then, * GET /<resource name> HTTP/1.1 .... * we should parse the specific resource and react appropriately */ // read incoming data until we read a space character PSOCK_READTO(&s->p, ISO_space); // parse the data to determine if it was a GET request if(strncmp(s->inputbuf, http_get, 4) != 0) { PSOCK_CLOSE_EXIT(&s->p); } // continue reading until the next space character PSOCK_READTO(&s->p, ISO_space); // determine the requested resource // in this case, we check if the request was for the '/' root page // AKA index.html if(s->inputbuf[0] != ISO_slash) { // request for unknown webpage, close and exit PSOCK_CLOSE_EXIT(&s->p); } // This is the web page served by the webserver PSOCK_SEND_STR(&s->p, "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\r\n"); PSOCK_SEND_STR(&s->p, "<HTML>\r\n<HEAD>\r\n\t<META http-equiv=\"Content-type\" content=\"text/html;charset=UTF-8\">\r\n\t<TITLE>Arduino WiShield 2.0</TITLE>\r\n"); PSOCK_SEND_STR(&s->p, "\t<STYLE type=\"text/css\">\r\n\t\tP,H1 {text-align: center;}\r\n\t</STYLE>\r\n</HEAD>\r\n<BODY>\r\n"); PSOCK_SEND_STR(&s->p, "\t<H1>Hello World!! I am an Arduino with a WiShield 2.0</H1>\r\n\t<P><A href=\"T\">Toggle LED</A></P>\r\n"); if(s->inputbuf[1] != ISO_space) { // request for a resource if(s->inputbuf[1] != ISO_T) { // not supported PSOCK_SEND_STR(&s->p, "\t<P>Request not supported</P>\r\n"); } else { PSOCK_SEND_STR(&s->p, "\t<P>TOGGLE LED command received</P>\r\n"); // Toggle WiShield 2.0 Led status digitalWrite(9, !digitalRead(9)); } } PSOCK_SEND_STR(&s->p, "</BODY>\r\n</HTML>"); PSOCK_CLOSE(&s->p); PSOCK_END(&s->p); }
/* * This is the protosocket function that handles the communication. A * protosocket function must always return an int, but must never * explicitly return - all return statements are hidden in the PSOCK * macros. */ static int handle_connection(struct socket_app_state *s) { PSOCK_BEGIN(&s->p); PSOCK_SEND_STR(&s->p, "Hello. What is you name?\n"); PSOCK_READTO(&s->p, '\n'); PSOCK_SEND_STR(&s->p, "Hello "); PSOCK_SEND_STR(&s->p, s->inputbuffer); memset(s->inputbuffer, 0x00, sizeof(s->inputbuffer)); PSOCK_CLOSE(&s->p); PSOCK_END(&s->p); }
static int handle_connection(struct socket_app_state *s) { PSOCK_BEGIN(&s->p); while(1){ PSOCK_READTO(&s->p, '\n'); strin = s->inputbuffer; memset(s->inputbuffer, 0x00, sizeof(s->inputbuffer)); PSOCK_SEND_STR(&s->p, strout); } PSOCK_CLOSE(&s->p); PSOCK_END(&s->p); }
static PT_THREAD(handle_connection(struct psock *p)) { PSOCK_BEGIN(p); while (true) { if (PSOCK_NEWDATA(p)) PSOCK_CLOSE(p); else PSOCK_SEND_STR(p,msg); } PSOCK_END(p); }
/* * This is the protosocket function that handles the communication. A * protosocket function must always return an int, but must never * explicitly return - all return statements are hidden in the PSOCK * macros. */ static int handle_connection(struct hello_world_state *s) { PSOCK_BEGIN(&p); PSOCK_SEND_STR(&p, "Hello. What is your name?\n"); PSOCK_READTO(&p, '\n'); strncpy(s->name, s->inputbuffer, sizeof(s->name)); PSOCK_SEND_STR(&p, "Hello "); PSOCK_SEND_STR(&p, s->name); PSOCK_CLOSE(&p); PSOCK_END(&p); }
/* * This is the protosocket function that handles the communication. A * protosocket function must always return an int, but must never * explicitly return - all return statements are hidden in the PSOCK * macros. */ static int handle_connection(struct socket_app_state *s){ PSOCK_BEGIN(&s->p); while(1){ PSOCK_READTO(&s->p, '\n'); if(*s->inputbuffer=='q'){ PSOCK_SEND_STR(&s->p, "\n"); break; } handleCommand(s->inputbuffer, s->outputbuffer); memset(s->inputbuffer, 0x00, SOCKET_BUFFER_LENGTH); PSOCK_SEND_STR(&s->p, s->outputbuffer); } PSOCK_CLOSE(&s->p); PSOCK_END(&s->p); }
static PT_THREAD(handle_output(struct httpd_state *s)) { PT_BEGIN(&s->outputpt); #if DEBUGLOGIC strcpy_P(s->filename,PSTR("/x")); #endif #if FIND_THE_SCRIPT s->script = httpd_simple_get_script(&s->filename[1]); if(s->script == NULL) { printf_P(PSTR("not found!")); strcpy_P(s->filename, PSTR("/notfound.html")); PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404)); PT_WAIT_THREAD(&s->outputpt, send_string_P(s, NOT_FOUND)); uip_close(); PT_EXIT(&s->outputpt); } else { #else s->script = generate_routes; if (1) { #endif PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200)); PT_WAIT_THREAD(&s->outputpt, s->script(s)); } s->script = NULL; PSOCK_CLOSE(&s->sout); PT_END(&s->outputpt); } /*---------------------------------------------------------------------------*/ static void handle_connection(struct httpd_state *s) { #if DEBUGLOGIC handle_output(s); #else handle_input(s); if(s->state == STATE_OUTPUT) { handle_output(s); } #endif }
static int handle_connection(struct webserver_state *s) { PSOCK_BEGIN(&s->p); // the incoming GET request will have the following format: // GET / HTTP/1.1 .... // we have to parse this string to determine the resource being requested // if the requested resource is not the root webpage ('/') then, // GET /<resource name> HTTP/1.1 .... // we should parse the specific resource and react appropriately // read incoming data until we read a space character PSOCK_READTO(&s->p, ISO_space); // parse the data to determine if it was a GET request if(strncmp(s->inputbuf, http_get, 4) != 0) { PSOCK_CLOSE_EXIT(&s->p); } // continue reading until the next space character PSOCK_READTO(&s->p, ISO_space); // determine the requested resource // in this case, we check if the request was for the '/' root page // AKA index.html if(s->inputbuf[0] != ISO_slash) { // request for unknown webpage, close and exit PSOCK_CLOSE_EXIT(&s->p); } if(s->inputbuf[1] != ISO_space) { // request for unavailable resource // not supported, modify to add support for additional resources PSOCK_CLOSE_EXIT(&s->p); } lockstate = lockstate+1; PSOCK_SEND_STR(&s->p, "HTTP/1.1 200 OK\r\n"); PSOCK_SEND_STR(&s->p, "Content-Type: text/html\r\n"); PSOCK_SEND_STR(&s->p, "\r\n"); PSOCK_SEND_STR(&s->p, "Hello World, I am WiShield"); PSOCK_SEND_STR(&s->p, "<center><h1>Hello World!! I am Matt's WiShield. Find me in webserver.c under PSOCK_SEND_STR.</h1></center>"); PSOCK_GENERATOR_SEND(&s->p, fill_buf, 0); PSOCK_CLOSE(&s->p); PSOCK_END(&s->p); }
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); }
/*---------------------------------------------------------------------------*/ static PT_THREAD(handle_output(struct httpd_state *s)) { PT_BEGIN(&s->outputpt); s->script = NULL; s->script = httpd_simple_get_script(&s->filename[1]); PT_WAIT_THREAD(&s->outputpt, s->script(s)); //PT_WAIT_THREAD(&s->outputpt, send_headers(s, s->http_header ? s->http_header : HTTP_HEADER_200)); //s->http_header = NULL; //PT_WAIT_THREAD(&s->outputpt, send_payload(s)); s->http_output_payload[0] = 0; s->script = NULL; PSOCK_CLOSE(&s->sout); PT_END(&s->outputpt); }
int handle_connection(webserver_state* s) { PSOCK_BEGIN(&s->p); // Read HTTP verb (e.g. GET/POST) - and ignore it. PSOCK_READTO(&s->p, ' '); // Read requested path (up to next space) PSOCK_READTO(&s->p, ' '); RESPOND_STR(s, "HTTP/1.1 200 OK\r\n"); RESPOND_STR(s, "Content-Type: text/plain\r\n"); RESPOND_STR(s, "\r\n"); if (STR_EQ(s->inputbuf, "/ ")) { // no-op, just for testing RESPOND_STR(s, "test"); } else if (STR_EQ(s->inputbuf, "/comcast ")) { room.watchComcast(); RESPOND_STR(s, "comcast"); } else if (STR_EQ(s->inputbuf, "/dvd ")) { room.watchDvd(); RESPOND_STR(s, "dvd"); } else if (STR_EQ(s->inputbuf, "/roku ")) { room.streamRoku(); RESPOND_STR(s, "roku"); } else if (STR_EQ(s->inputbuf, "/ps3 ")) { room.playPlaystation(); RESPOND_STR(s, "ps3"); } else if (STR_EQ(s->inputbuf, "/cd ")) { room.listenCd(); RESPOND_STR(s, "cd"); } else if (STR_EQ(s->inputbuf, "/off ")) { room.allOff(); RESPOND_STR(s, "off"); } else if (STR_EQ(s->inputbuf, "/reset ")) { room.forceReset(); RESPOND_STR(s, "reset"); } else { RESPOND_STR(s, "unknown"); } RESPOND_STR(s, "\r\n"); PSOCK_CLOSE(&s->p); PSOCK_END(&s->p); }
static uint16_t handle_connection(struct simple_httpd_state *s) { PSOCK_BEGIN(&s->p); int comp; // the incoming GET request will have the following format: // GET / HTTP/1.1 .... // we have to parse this string to determine the resource being requested // if the requested resource is not the root webpage ('/') then, // GET /<resource name> HTTP/1.1 .... // we should parse the specific resource and react appropriately // read incoming data until we read a space character PSOCK_READTO(&s->p, ISO_space); // parse the data to determine if it was a GET request if((comp = strncmp_P((const char *)(s->inputbuf), (const char *)http_get, 4)) != 0) { PSOCK_CLOSE_EXIT(&s->p); } // continue reading until the next space character PSOCK_READTO(&s->p, ISO_space); // determine the requested resource // in this case, we check if the request was for the '/' root page // AKA index.html if(s->inputbuf[0] != ISO_slash){ PSOCK_CLOSE_EXIT(&s->p); // request for unknown webpage, close and exit } // not supported, modify to add support for additional resources if(s->inputbuf[1] != ISO_space){ PSOCK_CLOSE_EXIT(&s->p); // request for unavailable resource, close and exit } PSOCK_GENERATOR_SEND(&s->p, fill_buf, 0); // generate the web page response with fill_buf from PSTR variable PSOCK_CLOSE(&s->p); PSOCK_END(&s->p); }
/* -------------------------------------------------------------------------- */ static PT_THREAD(handle_connection(struct psock *p)) { // So, as always let's start PSOCK_BEGIN(p); // Let's send a hello world PSOCK_SEND_STR(p, "Welcome, please type something and press return.\n"); // Let's block this thread untile a new line is received PSOCK_READTO(p, '\n'); // And what we receive, we send it back, MUAHAHA PSOCK_SEND_STR(p, "Got the following data: "); PSOCK_SEND(p, buffer, PSOCK_DATALEN(p)); PSOCK_SEND_STR(p, "Good bye!\r\n"); // Finally close the socket PSOCK_CLOSE(p); // The End... PSOCK_END(p); }
static int handle_connection(struct socket_app_state *s) { PSOCK_BEGIN(&s->p); // str2 = const_cast<char*>("cheney"); Serial.print("send "); // str1 = const_cast<char*>("hello"); Serial.print((const char* )hokuyo_data); PSOCK_SEND_STR(&s->p, hokuyo_data); // delay(5000); // PSOCK_READTO(&s->p, '\n'); // Serial.println("after readto"); // PSOCK_SEND_STR(&s->p, "Hello\n"); // PSOCK_SEND_STR(&s->p, s->inputbuffer); // memset(s->inputbuffer, 0x00, sizeof(s->inputbuffer)); PSOCK_CLOSE(&s->p); PSOCK_END(&s->p); }