Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of webserverBlinky by
Diff: main.cpp
- Revision:
- 98:3babad0d1bd4
- Parent:
- 97:bdf885e146dc
- Child:
- 99:3f56162e703e
--- a/main.cpp Sat Jul 29 00:40:00 2017 +0000 +++ b/main.cpp Sat Jul 29 23:28:19 2017 +0000 @@ -102,7 +102,7 @@ unsigned int seq; // our TCP sequence number int crc; // for calculating IP and TCP CRCs int ledState; // state of LED1 - int httpFrameCount; + int httpPageCount; struct { #define RXBUFLEN (1<<14) // the serial port receive buffer and packet buffer, size is RXBUFLEN (currently 8192 bytes) @@ -141,7 +141,7 @@ ppp.ledState=0; ppp.hdlc.frameFound=0; ppp.hdlc.frameStartIndex=0; - ppp.httpFrameCount=0; + ppp.httpPageCount=0; } void led1Toggle() @@ -554,10 +554,10 @@ int contentLengthStart; // index where HTML starts int xFetch, httpGetRoot; // temporary storage of strncmp results - ppp.httpFrameCount++; // increment the number of frames we have made + ppp.httpPageCount++; // increment the number of frames we have made - httpGetRoot = strncmp(dataStart, "GET / HTTP/1.1", 14); // found GET / - xFetch = strncmp(dataStart, "GET /x HTTP/1.1", 15); // found GET /x + httpGetRoot = strncmp(dataStart, "GET / HTTP/1.", 13); // found GET, respond to both HTTP/1.<anything> + xFetch = strncmp(dataStart, "GET /x HTTP/1.", 14); // found GET /x , respond to both HTTP/1.<anything> if( (httpGetRoot==0) || (xFetch==0) ) { n=n+sprintf(n+dataStart,"HTTP/1.1 200 OK\r\nServer: mbed-PPP-Blinky\r\n"); // 200 OK header } else { @@ -581,9 +581,15 @@ // change the above to W3C_COMPLIANT_RESPONSE_YES if you want a W3C.org compliant HTTP response #ifdef W3C_COMPLIANT_RESPONSE_YES n=n+sprintf(n+dataStart,"<!DOCTYPE html><title>mbed-ppp-blinky</title>"); // html title (W3C.org required element) - n=n+sprintf(n+dataStart,"<body>%d</body>",ppp.httpFrameCount); // body = the http frame count + n=n+sprintf(n+dataStart,"<body>%d</body>",ppp.httpPageCount); // body = the http frame count #else - n=n+sprintf(n+dataStart,"%d",ppp.httpFrameCount); // not valid html but fast, most browsers and curl are ok with it +#define BENCHMARK_USING_BROWSER_NO /* set to _YES if you want to use your browser as a benchmark tool of http fetches */ +#ifndef BENCHMARK_USING_BROWSER_NO + // a small script that reloads the page after 10 ms - handy for benchmarking using your web browser + n=n+sprintf(n+dataStart, "<script>setTimeout(function(){location.reload();},10);</script><body>%d</body>",ppp.httpPageCount); +#else + n=n+sprintf(n+dataStart,"%d",ppp.httpPageCount); // not valid html but fast, most browsers and curl are ok with it +#endif #endif } else { @@ -606,6 +612,24 @@ return n; // total byte size of our response } +// if not an http response we just report the number of bytes received +// this is handy when you for example want to use netcat (nc.exe) to talk to PPP-Blinky +int tcpResponse(char * dataStart, int len) +{ + int n=0; // number of bytes we have printed so far + + n=n+sprintf(n+dataStart,"Got %04d bytes.\n",len); // report the number of bytes received + while( (n%4)!= 0) n=n+sprintf(n+dataStart,"*"); // insert spaces until n is exactly two away from a multiple of four + if (v2) { + debug("TCP response %d byes\n",n); + } + return n; // total byte size of our response +} + + + + + void tcpHandler() { @@ -685,17 +709,21 @@ ack_out++; // for SYN flag we have to increase the sequence by 1 break; case TCP_FLAG_ACK | TCP_FLAG_PSH: - flagsOut = TCP_FLAG_ACK ; //| TCP_FLAG_FIN; // for every push we answer once AND close the link - if ( strncmp(tcpDataIn, "GET ", 4) == 0) { // check for an http GET command + if ( strncmp(tcpDataIn, "GET /", 5) == 0) { // check for an http GET command + flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN; // set the FIN flag to start closing this TCP connection dataLen = httpResponse(tcpDataOut); // send an http response + } else { + dataLen = tcpResponse(tcpDataOut,tcpDataSize); // not a web request, just report number of received bytes } break; - case TCP_FLAG_FIN: + case TCP_FLAG_FIN | TCP_FLAG_PSH | TCP_FLAG_ACK: case TCP_FLAG_FIN | TCP_FLAG_ACK: case TCP_FLAG_RST: - doFin = 1; // they want to tear down the link, so we must send a FIN packet also ack_out++; // for FIN flag we always have to increase sequence by 1 break; + case TCP_FLAG_FIN: + flagsOut = TCP_FLAG_ACK | TCP_FLAG_FIN; // set the FIN flag to start closing the connection + break; default: return; // ignore remaining packets } // switch