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@126:62edacc9f14d, 2019-02-25 (annotated)
- Committer:
- andrewboyson
- Date:
- Mon Feb 25 20:59:19 2019 +0000
- Revision:
- 126:62edacc9f14d
- Parent:
- 117:a725e5ad4fad
- Child:
- 129:9c57197fb698
Added ability to post content plus content length and content start .
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 61:aad055f1b0d1 | 1 | #include <stdbool.h> |
andrewboyson | 61:aad055f1b0d1 | 2 | |
andrewboyson | 54:84ef2b29cf7e | 3 | #include "http.h" |
andrewboyson | 54:84ef2b29cf7e | 4 | #include "tcpbuf.h" |
andrewboyson | 54:84ef2b29cf7e | 5 | #include "action.h" |
andrewboyson | 54:84ef2b29cf7e | 6 | #include "net.h" |
andrewboyson | 54:84ef2b29cf7e | 7 | #include "log.h" |
andrewboyson | 75:603b10404183 | 8 | #include "led.h" |
andrewboyson | 98:b977424ec7f7 | 9 | #include "fault.h" |
andrewboyson | 54:84ef2b29cf7e | 10 | |
andrewboyson | 54:84ef2b29cf7e | 11 | bool HttpTrace = false; |
andrewboyson | 54:84ef2b29cf7e | 12 | |
andrewboyson | 126:62edacc9f14d | 13 | //Plumb into these from your html server |
andrewboyson | 126:62edacc9f14d | 14 | int (*HttpRequestFunction)(char *pPath, char *pLastModified); |
andrewboyson | 126:62edacc9f14d | 15 | void (*HttpGetFunction )(int todo, char *pQuery); |
andrewboyson | 126:62edacc9f14d | 16 | bool (*HttpPostFunction )(int todo, int contentLength, int contentStart, int size, char* pRequestStream, uint32_t positionInRequestStream); |
andrewboyson | 126:62edacc9f14d | 17 | void (*HttpReplyFunction )(int todo); |
andrewboyson | 54:84ef2b29cf7e | 18 | |
andrewboyson | 126:62edacc9f14d | 19 | void HttpHandleRequest(int size, char* pRequestStream, uint32_t positionInRequestStream, int* pToDo, bool* pPostComplete) |
andrewboyson | 54:84ef2b29cf7e | 20 | { |
andrewboyson | 98:b977424ec7f7 | 21 | int lastFaultPoint = FaultPoint; |
andrewboyson | 98:b977424ec7f7 | 22 | FaultPoint = FAULT_POINT_HttpHandleRequest; |
andrewboyson | 98:b977424ec7f7 | 23 | |
andrewboyson | 54:84ef2b29cf7e | 24 | if (HttpTrace) |
andrewboyson | 54:84ef2b29cf7e | 25 | { |
andrewboyson | 79:f50e02fb5c94 | 26 | LogF("HTTP <<< %d (%u)\r\n", size, positionInRequestStream); |
andrewboyson | 54:84ef2b29cf7e | 27 | } |
andrewboyson | 54:84ef2b29cf7e | 28 | //Handle request for the first packet of data received but leave todo the same after that. |
andrewboyson | 126:62edacc9f14d | 29 | int contentLength = 0; |
andrewboyson | 126:62edacc9f14d | 30 | int contentStart = 0; |
andrewboyson | 75:603b10404183 | 31 | if (size && positionInRequestStream == 0) |
andrewboyson | 54:84ef2b29cf7e | 32 | { |
andrewboyson | 126:62edacc9f14d | 33 | //Read the headers |
andrewboyson | 54:84ef2b29cf7e | 34 | char* pMethod; |
andrewboyson | 54:84ef2b29cf7e | 35 | char* pPath; |
andrewboyson | 54:84ef2b29cf7e | 36 | char* pQuery; |
andrewboyson | 54:84ef2b29cf7e | 37 | char* pLastModified; |
andrewboyson | 108:66f17386ffd4 | 38 | FaultPoint = FAULT_POINT_HttpReadRequest; |
andrewboyson | 126:62edacc9f14d | 39 | contentStart = HttpRequestRead(pRequestStream, size, &pMethod, &pPath, &pQuery, &pLastModified, &contentLength); |
andrewboyson | 126:62edacc9f14d | 40 | |
andrewboyson | 126:62edacc9f14d | 41 | //Ask the http server what to do |
andrewboyson | 108:66f17386ffd4 | 42 | FaultPoint = FAULT_POINT_HttpRequestFunction; |
andrewboyson | 126:62edacc9f14d | 43 | *pToDo = HttpRequestFunction(pPath, pLastModified); |
andrewboyson | 126:62edacc9f14d | 44 | |
andrewboyson | 126:62edacc9f14d | 45 | //Handle the query |
andrewboyson | 126:62edacc9f14d | 46 | FaultPoint = FAULT_POINT_HttpGetFunction; |
andrewboyson | 126:62edacc9f14d | 47 | HttpGetFunction(*pToDo, pQuery); |
andrewboyson | 54:84ef2b29cf7e | 48 | } |
andrewboyson | 126:62edacc9f14d | 49 | |
andrewboyson | 126:62edacc9f14d | 50 | //Do the upload of anything that needs it |
andrewboyson | 126:62edacc9f14d | 51 | FaultPoint = FAULT_POINT_HttpPostFunction; |
andrewboyson | 126:62edacc9f14d | 52 | if (!*pPostComplete) *pPostComplete = HttpPostFunction(*pToDo, contentLength, contentStart, size, pRequestStream, positionInRequestStream); |
andrewboyson | 126:62edacc9f14d | 53 | |
andrewboyson | 126:62edacc9f14d | 54 | //Tidy up |
andrewboyson | 98:b977424ec7f7 | 55 | FaultPoint = lastFaultPoint; |
andrewboyson | 71:736a5747ade1 | 56 | } |
andrewboyson | 79:f50e02fb5c94 | 57 | void HttpSendReply(int* pSize, char* pReplyStream, uint32_t positionInReplyStream, uint16_t mss, int todo) |
andrewboyson | 71:736a5747ade1 | 58 | { |
andrewboyson | 54:84ef2b29cf7e | 59 | TcpBufStart(positionInReplyStream, mss, pReplyStream); |
andrewboyson | 71:736a5747ade1 | 60 | HttpReplyFunction(todo); |
andrewboyson | 54:84ef2b29cf7e | 61 | *pSize = TcpBufLength(); |
andrewboyson | 55:e64b8b47a2b6 | 62 | |
andrewboyson | 55:e64b8b47a2b6 | 63 | if (HttpTrace) |
andrewboyson | 55:e64b8b47a2b6 | 64 | { |
andrewboyson | 55:e64b8b47a2b6 | 65 | LogF("HTTP >>> %d (%d)\r\n", *pSize, positionInReplyStream); |
andrewboyson | 55:e64b8b47a2b6 | 66 | } |
andrewboyson | 71:736a5747ade1 | 67 | } |