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

tcp/http/http.c

Committer:
andrewboyson
Date:
2019-02-25
Revision:
126:62edacc9f14d
Parent:
117:a725e5ad4fad
Child:
129:9c57197fb698

File content as of revision 126:62edacc9f14d:

#include <stdbool.h>

#include   "http.h"
#include "tcpbuf.h"
#include "action.h"
#include    "net.h"
#include    "log.h"
#include    "led.h"
#include  "fault.h"

bool HttpTrace = false;

//Plumb into these from your html server
int  (*HttpRequestFunction)(char *pPath, char *pLastModified);
void (*HttpGetFunction    )(int todo, char *pQuery);
bool (*HttpPostFunction   )(int todo, int contentLength, int contentStart, int size, char* pRequestStream, uint32_t positionInRequestStream);
void (*HttpReplyFunction  )(int todo);

void HttpHandleRequest(int size, char* pRequestStream, uint32_t positionInRequestStream, int* pToDo, bool* pPostComplete)
{
    int lastFaultPoint = FaultPoint;
    FaultPoint = FAULT_POINT_HttpHandleRequest;
    
    if (HttpTrace)
    {
        LogF("HTTP  <<< %d (%u)\r\n", size, positionInRequestStream);
    }
    //Handle request for the first packet of data received but leave todo the same after that.
    int contentLength = 0;
    int contentStart  = 0;
    if (size && positionInRequestStream == 0)
    {
        //Read the headers
        char* pMethod;
        char* pPath;
        char* pQuery;
        char* pLastModified;
        FaultPoint = FAULT_POINT_HttpReadRequest;
        contentStart = HttpRequestRead(pRequestStream, size, &pMethod, &pPath, &pQuery, &pLastModified, &contentLength);
        
        //Ask the http server what to do
        FaultPoint = FAULT_POINT_HttpRequestFunction;
        *pToDo = HttpRequestFunction(pPath, pLastModified);
        
        //Handle the query
        FaultPoint = FAULT_POINT_HttpGetFunction;
        HttpGetFunction(*pToDo, pQuery);
    }
    
    //Do the upload of anything that needs it
    FaultPoint = FAULT_POINT_HttpPostFunction;
    if (!*pPostComplete) *pPostComplete = HttpPostFunction(*pToDo, contentLength, contentStart, size, pRequestStream, positionInRequestStream);
    
    //Tidy up
    FaultPoint = lastFaultPoint;
}
void HttpSendReply(int* pSize, char* pReplyStream, uint32_t positionInReplyStream, uint16_t mss, int todo)
{
    TcpBufStart(positionInReplyStream, mss, pReplyStream);
    HttpReplyFunction(todo);
    *pSize = TcpBufLength();

    if (HttpTrace)
    {
        LogF("HTTP  >>> %d (%d)\r\n", *pSize, positionInReplyStream);
    }
}