A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.
Dependents: oldheating gps motorhome heating
Diff: tcp/http.cpp
- Revision:
- 10:f0854784e960
- Parent:
- 4:31fa7d50722c
- Child:
- 21:02c82594c8c0
diff -r 91dae5300a4d -r f0854784e960 tcp/http.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tcp/http.cpp Sun Apr 16 14:21:55 2017 +0000 @@ -0,0 +1,136 @@ +#include "mbed.h" +#include "http.h" +#include "time.h" + + +int (*HttpRequestFunction)(char *pPath, char *pLastModified); //Plumb into this from your html server +int (*HttpResponseFunction)(int position, int mss, char *pData, int todo); //Plumb into this from your html server + +static void dateHeaderFromTm(struct tm* ptm, char* ptext) +{ + size_t size = strftime(ptext, HTTP_DATE_LENGTH, "%a, %d %b %Y %H:%M:%S GMT", ptm);//Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123 +} +void HttpDateHeaderFromNow(char* pText) +{ + struct tm tm; + TimeToTmUtc(time(NULL), &tm); + dateHeaderFromTm(&tm, pText); +} +void HttpDateHeaderFromDateTime(const char* date, const char *ptime, char* ptext) +{ + struct tm tm; + TimeAsciiDateTimeToTm(date, ptime, &tm); + dateHeaderFromTm(&tm, ptext); +} + +static char* getNextLine(char* p, char* pE) //Terminates this line and returns the start of the next line or NULL if none +{ + while (true) + { + if (p == pE) //There are no more lines + { + *p = 0; //terminate the line + return NULL; + } + if (*p == 0) return NULL;//There are no more lines + if (*p == '\n') + { + *p = 0; //make the line a c string + return p + 1; //return the start of the next line + } + if (*p < ' ') *p = 0; //terminate the line at any invalid characters + if (*p >= 0x7f) *p = 0; //terminate the line at any invalid characters + p++; + } +} + +static void splitRequest(char* p, char** ppMethod, char** ppPath, char** ppQuery) +{ + *ppMethod = NULL; + *ppPath = NULL; + *ppQuery = NULL; + + while (*p == ' ') //Move past any leading spaces + { + if (*p == 0) return; + p++; + } + *ppMethod = p; //Record the start of the method (GET or POST) + + while (*p != ' ') //Move past the method + { + if (*p == 0) return; + p++; + } + *p = 0; //Terminate the method + p++; //Start at next character + + while (*p == ' ') //Move past any spaces + { + if (*p == 0) return; + p++; + } + *ppPath = p; //Record the start of the path + + while (*p != ' ') //Move past the path and query + { + if (*p == 0) return; + if (*p == '?') + { + *p = 0; //Terminate the path + *ppQuery = p + 1; //Record the start of the query + } + p++; + } + *p = 0; //Terminate the path or query +} +static void splitHeader(char* p, char** ppName, char** ppValue) +{ + *ppName = p; //Record the start of the name + *ppValue = NULL; + + while (*p != ':') //Loop to an ':' + { + if (!*p) return; + p++; + } + *p = 0; //Terminate the name by replacing the ':' with a NUL char + p++; + while (*p == ' ') //Move past any spaces + { + if (*p == 0) return; + p++; + } + *ppValue = p; //Record the start of the value +} + +int HttpRequest(int position, int len, char *pData) +{ + char* pMethod; + char* pPath; + char* pQuery; + + char* pEnd = pData + len; + char* pThis = pData; + char* pNext = getNextLine(pThis, pEnd); + splitRequest(pThis, &pMethod, &pPath, &pQuery); + + char* pLastModified; + while(pNext) + { + pThis = pNext; + pNext = getNextLine(pThis, pEnd); + if (*pThis == 0) break; //This line is empty ie no more headers + char* pName; + char* pValue; + splitHeader(pThis, &pName, &pValue); + if (strcmp(pName, "If-Modified-Since") == 0) pLastModified = pValue; + } + + return HttpRequestFunction(pPath, pLastModified); +} +int HttpResponse(int position, int mss, char *pData, int todo) +{ + return HttpResponseFunction(position, mss, pData, todo); +} +