Advertisement
Guest User

Web server code snippet

a guest
Dec 15th, 2015
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.09 KB | None | 0 0
  1. //webServer is called each tick, all variable not declared in this snippet are declared in a separate header file.
  2. void webServer(){
  3. EthernetClient client = server.available(); //Listens on port 80
  4. if (client) {  // got client?
  5.         boolean currentLineIsBlank = true;
  6.         while (client.connected()) {
  7.             if (client.available()) {   // client data available to read
  8.                 char c = client.read(); // read 1 byte (character) from client
  9.                 // buffer first part of HTTP request in HTTP_req array (string)
  10.                 // leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
  11.                 if (req_index < (REQ_BUF_SZ - 1)) {
  12.                     HTTP_req[req_index] = c;          // save HTTP request character
  13.                     req_index++;
  14.                 }
  15.                 // last line of client request is blank and ends with \n
  16.                 // respond to client only after last line received
  17.                 if (c == '\n' && currentLineIsBlank) {
  18.                     // open requested web page file
  19.                     if (StrContains(HTTP_req, "GET / ")
  20.                                  || StrContains(HTTP_req, "GET /index.htm")) {
  21.                         client.println(F("HTTP/1.1 200 OK"));
  22.                         client.println(F("Content-Type: text/html"));
  23.                         client.println(F("Connnection: close"));
  24.                         client.println();
  25.                         webFile = SD.open("index.htm");        // open web page file
  26.                     }
  27.                     else if (StrContains(HTTP_req, "GET /logo.jpg")) {
  28.                         webFile = SD.open("logo.jpg");
  29.                         if (webFile) {
  30.                             client.println(F("HTTP/1.1 200 OK"));
  31.                             client.println();
  32.                         }
  33.                     }
  34.                     if (webFile) {
  35.                         while(webFile.available()) {
  36.                             client.write(webFile.read()); // send web page to client
  37.                         }
  38.                         webFile.close();
  39.                     }
  40.                     else
  41.                     {
  42.                       client.println(F("Error, please Check;")); //Catch any error code and report if unable to find files.
  43.                       client.println(dStatus); //Error code is int 1-6, indicates what file was missing and if SD initialized
  44.                     }
  45.                     // reset buffer index and all buffer elements to 0
  46.                     req_index = 0;
  47.                     StrClear(HTTP_req, REQ_BUF_SZ);
  48.                     break;
  49.                 }
  50.                 // every line of text received from the client ends with \r\n
  51.                 if (c == '\n') {
  52.                     // last character on line of received text
  53.                     // starting new line with next character read
  54.                     currentLineIsBlank = true;
  55.                 }
  56.                 else if (c != '\r') {
  57.                     // a text character was received from client
  58.                     currentLineIsBlank = false;
  59.                 }
  60.             } // end if (client.available())
  61.         } // end while (client.connected())
  62.         delay(1);      // give the web browser time to receive the data
  63.         client.stop(); // close the connection
  64.     } // end if (client)
  65. }
  66.  
  67. // sets every element of str to 0 (clears array)
  68. void StrClear(char *str, char length)
  69. {
  70.     for (int i = 0; i < length; i++) {
  71.         str[i] = 0;
  72.     }
  73. }
  74.  
  75. // searches for the string sfind in the string str
  76. // returns 1 if string found
  77. // returns 0 if string not found
  78. char StrContains(char *str, char *sfind)
  79. {
  80.     char found = 0;
  81.     char index = 0;
  82.     char len;
  83.  
  84.     len = strlen(str);
  85.    
  86.     if (strlen(sfind) > len) {
  87.         return 0;
  88.     }
  89.     while (index < len) {
  90.         if (str[index] == sfind[found]) {
  91.             found++;
  92.             if (strlen(sfind) == found) {
  93.                 return 1;
  94.             }
  95.         }
  96.         else {
  97.             found = 0;
  98.         }
  99.         index++;
  100.     }
  101.  
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement