int do_http_post(int sd, char *req, int rlen) { int BUFSIZE = 1024; char buf[BUFSIZE]; int len = 0, n; char *p; if (is_cmd_led(req)) { n = toggle_leds(); len = generate_http_header(buf, "txt", 1); p = buf + len; *p++ = n?'1':'0'; *p = 0; len++; xil_printf("http POST: ledstatus: %x\r\n", n); } else if (is_cmd_switch(req)) { unsigned s = get_switch_state(); int n_switches = 8; xil_printf("http POST: switch state: %x\r\n", s); len = generate_http_header(buf, "txt", n_switches); p = buf + len; for (n = 0; n < n_switches; n++) { *p++ = '0' + (s & 0x1); s >>= 1; } *p = 0; len += n_switches; } else {
/* dynamically generate 404 response: * this inserts the original request string in betwween the notfound_header & footer strings */ int do_404(struct tcp_pcb *pcb, char *req, int rlen) { int len, hlen; int BUFSIZE = 1024; char buf[BUFSIZE]; err_t err; len = strlen(notfound_header) + strlen(notfound_footer) + rlen; hlen = generate_http_header(buf, "html", len); if (tcp_sndbuf(pcb) < hlen) { xil_printf("cannot send 404 message, tcp_sndbuf = %d bytes, message length = %d bytes\r\n", tcp_sndbuf(pcb), hlen); return -1; } if ((err = tcp_write(pcb, buf, hlen, 1)) != ERR_OK) { xil_printf("%s: error (%d) writing 404 http header\r\n", __FUNCTION__, err); return -1; } tcp_write(pcb, notfound_header, strlen(notfound_header), 1); tcp_write(pcb, req, rlen, 1); tcp_write(pcb, notfound_footer, strlen(notfound_footer), 1); return 0; }
int do_http_post(struct tcp_pcb *pcb, char *req, int rlen) { int BUFSIZE = 1024; char buf[BUFSIZE]; int len, n; char *p; if (is_cmd_led(req)) { n = toggle_leds(); len = generate_http_header(buf, "js", 1); p = buf + len; *p++ = n?'1':'0'; *p = 0; len++; xil_printf("http POST: ledstatus: %x\r\n", n); } else if (is_cmd_switch(req)) { unsigned s = get_switch_state(); int n_switches = 8; //char *json_response = "{\"status\":\"10101011\"}"; xil_printf("http POST: switch state: %x\r\n", s); len = generate_http_header(buf, "js", n_switches); //len = generate_http_header(buf, "js", strlen(json_response)); p = buf + len; #if 1 for (n = 0; n < n_switches; n++, p++) { *p = '0' + (s & 0x1); s >>= 1; } *p = 0; len += n_switches; #else strcpy(p, json_response); len += strlen(json_response); #endif } if (tcp_write(pcb, buf, len, 1) != ERR_OK) { xil_printf("error writing http POST response to socket\n\r"); xil_printf("http header = %s\r\n", buf); return -1; } return 0; }
int do_http_post(struct tcp_pcb* pcb, char* req, int rlen) { int BUFSIZE = 1024; unsigned char buf[BUFSIZE]; int len, n; char* p; if (is_cmd_led(req)) { n = toggle_leds(); len = generate_http_header((char*)buf, "js", 1); p = (char*)buf + len; *p++ = n ? '1':'0'; *p = 0; len++; xil_printf("http POST: ledstatus: %x\r\n", n); } else if (is_cmd_switch(req)) { unsigned s = get_switch_state(); int n_switches = 4; xil_printf("http POST: switch state: %x\r\n", s); len = generate_http_header((char*)buf, "js", n_switches); p = (char*)buf + len; for (n=0; n<n_switches; n++, p++) { *p = '0' + (s & 0x1); s >>= 1; } *p = 0; len += n_switches; } if (tcp_write(pcb, buf, len, 1) != ERR_OK) { xil_printf("error writing http POST response to socket\r\n"); xil_printf("http header = %s\r\n", buf); return -1; } return 0; }
/* int do_http_post(struct tcp_pcb *pcb, char *req, int rlen) { int BUFSIZE = 1024; unsigned char buf[BUFSIZE]; int len, n; char *p; if (is_cmd_led(req)) { n = toggle_leds(); len = generate_http_header((char *)buf, "js", 1); p = (char *)buf + len; *p++ = n?'1':'0'; *p = 0; len++; xil_printf("http POST: ledstatus: %x\r\n", n); } else if (is_cmd_switch(req)) { unsigned s = get_switch_state(); int n_switches = 8; xil_printf("http POST: switch state: %x\r\n", s); len = generate_http_header((char *)buf, "js", n_switches); p = (char *)buf + len; for (n = 0; n < n_switches; n++, p++) { *p = '0' + (s & 0x1); s >>= 1; } *p = 0; len += n_switches; } if (tcp_write(pcb, buf, len, 1) != ERR_OK) { xil_printf("error writing http POST response to socket\n\r"); xil_printf("http header = %s\r\n", buf); return -1; } return 0; } */ int do_virtual_responce(struct tcp_pcb *pcb,char *req_fname) { int BUFSIZE = 1400; unsigned char buf[BUFSIZE]; unsigned char dbuf[BUFSIZE]; unsigned int buff_size; err_t err; http_get_pars( req_fname,dbuf,&buff_size); /* if buff size =0 set to 2 and retun OK */ if(buff_size == 0) { sprintf(dbuf,"OK"); buff_size=2; } /* write the http headers */ int hlen = generate_http_header((char *)buf,NULL, buff_size); if ((err = tcp_write(pcb, buf, hlen, 3)) != ERR_OK) { xil_printf("error (%d) writing http header to socket\r\n", err); xil_printf("attempted to write #bytes = %d, tcp_sndbuf = %d\r\n", hlen, tcp_sndbuf(pcb)); xil_printf("http header = %s\r\n", buf); return -1; } /* now write the file */ int sndbuf; sndbuf = tcp_sndbuf(pcb); if (sndbuf < buff_size) { //TODO change to virtual data add it to a /* not enough space in sndbuf, so send remaining bytes when there is space */ /* this is done by storing the fd in as part of the tcp_arg, so that the sent callback handler knows to send data * / http_arg *a = (http_arg *)pcb->callback_arg; a->fd = fd; a->fsize = fsize; */ return -1; } if ((err = tcp_write(pcb, dbuf, buff_size, 3)) != ERR_OK) { xil_printf("error (%d) writing http header to socket\r\n", err); xil_printf("attempted to write #bytes = %d, tcp_sndbuf = %d\r\n", hlen, tcp_sndbuf(pcb)); xil_printf("http header = %s\r\n", buf); return -1; } return 0; }
/* dynamically generate 404 response: * this inserts the original request string in betwween the notfound_header & footer strings */ int do_404(int sd, char *req, int rlen) { int len, hlen; int BUFSIZE = 1024; char buf[BUFSIZE]; len = strlen(notfound_header) + strlen(notfound_footer) + rlen; hlen = generate_http_header(buf, "html", len); if (lwip_write(sd, buf, hlen) != hlen) { xil_printf("error writing http header to socket\r\n"); xil_printf("http header = %s\n\r", buf); return -1; } lwip_write(sd, notfound_header, strlen(notfound_header)); lwip_write(sd, req, rlen); lwip_write(sd, notfound_footer, strlen(notfound_footer)); return 0; }
/* respond for a file GET request */ int do_http_get(struct tcp_pcb *pcb, char *req, int rlen) { int BUFSIZE = 1400; char filename[MAX_FILENAME]; char buf[BUFSIZE]; int fsize, hlen, n; int fd; char *fext; err_t err; /* determine file name */ extract_file_name(filename, req, rlen, MAX_FILENAME); /* respond with 404 if not present */ if (mfs_exists_file(filename) != 1) { xil_printf("requested file %s not found, returning 404\r\n", filename); do_404(pcb, req, rlen); return -1; } /* respond with correct file */ /* debug statement on UART */ xil_printf("http GET: %s\r\n", filename); /* get a pointer to file extension */ fext = get_file_extension(filename); fd = mfs_file_open(filename, MFS_MODE_READ); /* obtain file size, * note that lseek with offset 0, MFS_SEEK_END does not move file pointer */ fsize = mfs_file_lseek(fd, 0, MFS_SEEK_END); /* write the http headers */ hlen = generate_http_header(buf, fext, fsize); if ((err = tcp_write(pcb, buf, hlen, 1)) != ERR_OK) { xil_printf("error (%d) writing http header to socket\r\n", err); xil_printf("attempted to write #bytes = %d, tcp_sndbuf = %d\r\n", hlen, tcp_sndbuf(pcb)); xil_printf("http header = %s\r\n", buf); return -1; } /* now write the file */ while (fsize) { int sndbuf; sndbuf = tcp_sndbuf(pcb); if (sndbuf < BUFSIZE) { /* not enough space in sndbuf, so send remaining bytes when there is space */ /* this is done by storing the fd in as part of the tcp_arg, so that the sent callback handler knows to send data */ http_arg *a = (http_arg *)pcb->callback_arg; a->fd = fd; a->fsize = fsize; return 0; } n = mfs_file_read(fd, buf, BUFSIZE); if ((err = tcp_write(pcb, buf, n, 1)) != ERR_OK) { xil_printf("error writing file (%s) to socket, remaining unwritten bytes = %d\r\n", filename, fsize - n); xil_printf("attempted to lwip_write %d bytes, tcp write error = %d\r\n", n, err); break; } fsize -= n; } mfs_file_close(fd); return 0; }
/* respond for a file GET request */ int do_http_get(int sd, char *req, int rlen) { char *response; size_t response_size; FILE *stream = open_memstream(&response, &response_size); /* ***************** * HTTP header * *****************/ char http_header[1024]; // Chose this size randomly, it's more than enough generate_http_header(http_header, "html", 0); fprintf(stream, "%s", http_header); /* ***************** * HTML head * *****************/ fprintf(stream, "<HTML>\n"); fprintf(stream, "<HEAD>\n"); fprintf(stream, "<META HTTP-EQUIV=""refresh"" CONTENT=""10; URL=192.168.1.99"">\n"); fprintf(stream, "<STYLE>\n"); fprintf(stream, "table, th, td { border: 1px solid black; border-collapse: collapse; }\n"); fprintf(stream, "th, td { padding: 4px; }\n"); fprintf(stream, "</STYLE>\n"); fprintf(stream, "</HEAD>\n"); /* *********************** * System status * ***********************/ char *pagefmt = "<BODY>\n<CENTER><B>Xilinx VC707 System Status (10 s refresh)</B></CENTER><BR>\n" "Uptime: %d s<BR>" "Temperature = %0.1f C<BR>\n" "INT Voltage = %0.1f V<BR>\n" "AUX Voltage = %0.1f V<BR>\n" "BRAM Voltage = %0.1f V<BR>\n<HR>"; fprintf(stream,pagefmt,procStatus.uptime,procStatus.v7temp,procStatus.v7vCCINT,procStatus.v7vCCAUX,procStatus.v7vBRAM); #ifdef POLL_UPOD_TEMPS /* ******************* * uPod status * *******************/ fprintf(stream, "<CENTER><B>uPod Status</B></CENTER><BR>\n"); generate_upod_table(stream); fprintf(stream, "<HR>\n"); #endif /* ******************* * Bit error rates * *******************/ fprintf(stream, "<CENTER><B>Eyescan Data</B></CENTER><BR>\n"); int ch_to_display = parse_channel(req); generate_central_ber_table(stream, ch_to_display); fprintf(stream, "<br><br>\n"); fprintf(stream, "<form action=\"\" method=\"get\"> View Channel: <input type=\"text\" size=3 name=\"ch\" value=\"%d\"> <input type=\"submit\" value=\"Submit\"><br>", ch_to_display); fprintf(stream, "<br><br>\n"); generate_eyescan_table(stream, ch_to_display); fprintf(stream, "<HR>\n"); /* *********************** * Close HTML, send data * ***********************/ fprintf(stream,"</BODY>\n</HTML>\n"); fflush(stream); // xil_printf("%s\n", response); //DEBUG int w; if ((w = lwip_write(sd, response, response_size)) < 0 ) { xil_printf("error writing web page to socket\r\n"); xil_printf("attempted to lwip_write %d bytes, actual bytes written = %d\r\n", response_size, w); return -4; } fclose(stream); free(response); return 0; }