Common stuff for all my devices' web server pages: css, login, log, ipv4, ipv6, firmware update, clock, reset info etc.

Dependents:   oldheating gps motorhome heating

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers http.c Source File

http.c

00001 #include "http.h"
00002 #include "http-connection.h"
00003 #include "web.h"
00004 #include "mstimer.h"
00005 #include "log.h"
00006 
00007 bool HttpGetTrace()
00008 {
00009     return WebTrace;
00010 }
00011 
00012 void HttpReset(int connection)
00013 {
00014     HttpConnectionReset(connection);
00015 }
00016 bool HttpAdd(int connectionId, int* pWindowSize, char* pWindow, uint32_t windowPositionInStream)
00017 {
00018     HttpAddStart(windowPositionInStream, *pWindowSize, pWindow); //We have something to send so start the buffer
00019     *pWindowSize = 0;
00020     
00021     struct HttpConnection* pConnection = HttpConnectionOrNull(connectionId);
00022     if (!pConnection) return false; //Protects the gap between the connection being established and the request being started
00023     
00024     int todo = pConnection->toDo; //Make a copy so that we don't modify todo in the state
00025     
00026     WebAddResponse(todo);
00027     
00028     *pWindowSize = HttpAddLength();
00029     
00030     return !HttpAddFilled(); //If we haven't used a full buffer then we have finished
00031 }
00032 int HttpPoll(int connectionId, bool clientFinished)
00033 {
00034     struct HttpConnection* pConnection = HttpConnectionOrNull(connectionId);
00035     if (!pConnection) //Protects the gap between the connection being established and the request being started
00036     {
00037         if (clientFinished) return HTTP_FINISHED; //The client hasn't requested anything and never will so finish
00038         else                return HTTP_WAIT;     //The client hasn't requested anything yet but still could
00039     }
00040     
00041     if (!pConnection->toDo)
00042     {
00043         if (clientFinished) return HTTP_FINISHED; //The client hasn't requested anything and never will so finish
00044         else                return HTTP_WAIT;     //The client hasn't requested anything yet but still could
00045     }
00046     if (!pConnection->postComplete               ) return HTTP_WAIT;  //Wait for the request (usually a POST) to finish
00047     if (!MsTimerAbsolute(pConnection->delayUntil)) return HTTP_WAIT;  //Wait a while (usually after a LOGIN attempt)
00048     
00049     return HTTP_HAVE_SOMETHING_TO_SEND;
00050 }
00051 void HttpRequest(int connectionId, int windowSize, char* pWindow, uint32_t windowPositionInStream)
00052 {
00053     struct HttpConnection* pConnection;
00054     if (!windowPositionInStream)
00055     {
00056         pConnection = HttpConnectionNew(connectionId);
00057     }
00058     else
00059     {
00060         pConnection = HttpConnectionOrNull(connectionId);
00061         if (!pConnection)
00062         {
00063             LogTimeF("WebRequest - no connection corresponds to id %d\r\n", connectionId);
00064             return;
00065         }
00066     }
00067     
00068     pConnection->delayUntil = MsTimerCount; //Default to no delay unless modified;
00069     
00070     //Handle request for the first packet of data received but leave todo the same after that.
00071     int contentLength = 0;
00072     int contentStart  = 0;
00073     if (windowSize && windowPositionInStream == 0)
00074     {
00075         //Read the headers
00076         char* pMethod;
00077         char* pPath;
00078         char* pQuery;
00079         char* pLastModified;
00080         char* pCookies;
00081         contentStart = HttpRequestRead(pWindow, windowSize, &pMethod, &pPath, &pQuery, &pLastModified, &pCookies, &contentLength);
00082         
00083         //Ask the web server what to do
00084         pConnection->toDo = WebDecideWhatToDo(pPath, pLastModified);
00085         
00086         //Handle the query
00087         int stop = WebHandleQuery(pQuery, pCookies, &pConnection->toDo, &pConnection->delayUntil); //return -1 on stop; 0 on continue
00088         if (stop)
00089         {
00090             pConnection->postComplete = true;
00091             return;
00092         }
00093     }
00094     
00095     //Do the upload of anything that needs it. Todos it doesn't understand are ignored.
00096     if (!pConnection->postComplete) WebHandlePost(pConnection->toDo, contentLength, contentStart, windowSize, pWindow, windowPositionInStream, &pConnection->postComplete);
00097 }