static boolean getstate_handler(TinyWebServer& web_server) { web_server.send_error_code(200); web_server.send_content_type("text/xml"); web_server.send_content_type("Connection: keep-alive"); web_server.end_headers(); manage_toggle(web_server.get_path()); manage_setvalue(web_server.get_path()); Client& stream = web_server.get_client(); lduino.XML_State(stream); return true; }
static boolean file_handler(TinyWebServer& web_server) { const char *path = web_server.get_path(); Serial << "GET " << path << '\n'; if (strcmp(path, "/") == 0) path = "/index.html"; const struct httpd_fsdata_segment *segment; int len; bool use_gzip; if (find_file(path, &segment, &len, &use_gzip)) { web_server.send_error_code(200); web_server.send_last_modified("Wed, 21 Oct 2015 07:28:00 GMT"); web_server.send_content_type(web_server.get_mime_type_from_filename(path)); if (use_gzip) web_server.write("Content-Encoding: gzip\n"); web_server.end_headers(); while (segment && len > 0) { char *data = (char *)pgm_read_word(&(segment->data)); int i; int segment_len = min(len, httpd_fsdata_segment_len); while (segment_len > 0) { uint8_t buf[256]; int i; for (i = 0; i < segment_len && i < sizeof(buf); i++) { buf[i] = pgm_read_byte(data++); } segment_len -= i; len -= i; web_server.write(buf, i); } segment = (const struct httpd_fsdata_segment *)pgm_read_word(&(segment->next)); } } else { web_server.send_error_code(404); web_server.send_content_type("text/plain"); web_server.end_headers(); web_server.print("not found"); } return true; }
static void file_uploader_handler(TinyWebServer& web_server, TinyWebPutHandler::PutAction action, char* buffer, int size) { static char *boundary = NULL; static size_t boundary_len = 0; static CircularBuffer<char, 128> cb; static enum state { search_begin=0, search_data, in_data} st; static int file_size = 0; static char *fname; switch (action) { case TinyWebPutHandler::START: { fname = web_server.get_file_from_path(web_server.get_path()); const char *ct = web_server.get_header_value("Content-Type"); if (!ct) break; boundary = strstr(ct, "boundary="); if (!boundary) break; boundary += 5; boundary[0] = '\r'; boundary[1] = '\n'; boundary[2] = '-'; boundary[3] = '-'; boundary_len = strlen(boundary); D(Serial.print("**** Upload file ")); D(Serial.println(fname)); free(fname); st = search_begin; file_size = 0; break; } case TinyWebPutHandler::WRITE: for (int i = 0; i < size; i++) { D(Serial.write(*buffer)); cb.push(*buffer++); switch(st) { case search_begin: if (cb.remain() < boundary_len-2) break; if (cb.match(boundary+2, boundary_len-2)) { st = search_data; D(Serial.println("search_data")); cb.flush(); } else { cb.pop(); } break; case search_data: if (cb.remain() < 4) break; if (cb.match("\r\n\r\n", 4)) { st = in_data; D(Serial.println("in_data")); cb.flush(); } else { cb.pop(); } break; case in_data: if (cb.remain() < boundary_len) break; if (cb.match(boundary, boundary_len)) { st = search_data; D(Serial.println("search_data")); cb.flush(); } else { if (file_size == 0) lduino.ClearProgram(); char c = cb.pop(); file_size++; lduino.LoadProgramLine(c); } break; }; } break; case TinyWebPutHandler::END: D(Serial.println("**** END")); D(Serial.print("file size ")); D(Serial.println(file_size)); Client& client = web_server.get_client(); String status; if (file_size > 0 && lduino.getProgramReady()) { status += F("<font color='green'>New ladder program uploaded - file size: "); status += file_size; status += F(" bytes</font>"); } else { status += F("<font color='red'>Upload aborted</font>"); } main_page(client, status); break; } }