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/httpadd.c@145:206bf0d073c7, 2019-05-14 (annotated)
- Committer:
- andrewboyson
- Date:
- Tue May 14 15:09:39 2019 +0000
- Revision:
- 145:206bf0d073c7
- Child:
- 146:0fc66d610fd6
Added a shim module between the web server and TCP to switch between http (direct) and https (tls).
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 145:206bf0d073c7 | 1 | #include <time.h> |
andrewboyson | 145:206bf0d073c7 | 2 | #include <stdio.h> |
andrewboyson | 145:206bf0d073c7 | 3 | |
andrewboyson | 145:206bf0d073c7 | 4 | #include "http.h" |
andrewboyson | 145:206bf0d073c7 | 5 | |
andrewboyson | 145:206bf0d073c7 | 6 | void HttpFillChar (char c, int length) |
andrewboyson | 145:206bf0d073c7 | 7 | { |
andrewboyson | 145:206bf0d073c7 | 8 | while (length > 0) |
andrewboyson | 145:206bf0d073c7 | 9 | { |
andrewboyson | 145:206bf0d073c7 | 10 | HttpAddChar(c); |
andrewboyson | 145:206bf0d073c7 | 11 | length--; |
andrewboyson | 145:206bf0d073c7 | 12 | } |
andrewboyson | 145:206bf0d073c7 | 13 | } |
andrewboyson | 145:206bf0d073c7 | 14 | int HttpAddText (const char* text) |
andrewboyson | 145:206bf0d073c7 | 15 | { |
andrewboyson | 145:206bf0d073c7 | 16 | const char* start = text; |
andrewboyson | 145:206bf0d073c7 | 17 | while (*text) |
andrewboyson | 145:206bf0d073c7 | 18 | { |
andrewboyson | 145:206bf0d073c7 | 19 | HttpAddChar(*text); |
andrewboyson | 145:206bf0d073c7 | 20 | text++; |
andrewboyson | 145:206bf0d073c7 | 21 | } |
andrewboyson | 145:206bf0d073c7 | 22 | return text - start; |
andrewboyson | 145:206bf0d073c7 | 23 | } |
andrewboyson | 145:206bf0d073c7 | 24 | int HttpAddV (char *fmt, va_list argptr) |
andrewboyson | 145:206bf0d073c7 | 25 | { |
andrewboyson | 145:206bf0d073c7 | 26 | int size = vsnprintf(NULL, 0, fmt, argptr); //Find the size required |
andrewboyson | 145:206bf0d073c7 | 27 | char text[size + 1]; //Allocate enough memory for the size required with an extra byte for the terminating null |
andrewboyson | 145:206bf0d073c7 | 28 | vsprintf(text, fmt, argptr); //Fill the new buffer |
andrewboyson | 145:206bf0d073c7 | 29 | return HttpAddText(text); //Add the text |
andrewboyson | 145:206bf0d073c7 | 30 | } |
andrewboyson | 145:206bf0d073c7 | 31 | int HttpAddF (char *fmt, ...) |
andrewboyson | 145:206bf0d073c7 | 32 | { |
andrewboyson | 145:206bf0d073c7 | 33 | va_list argptr; |
andrewboyson | 145:206bf0d073c7 | 34 | va_start(argptr, fmt); |
andrewboyson | 145:206bf0d073c7 | 35 | int size = HttpAddV(fmt, argptr); |
andrewboyson | 145:206bf0d073c7 | 36 | va_end(argptr); |
andrewboyson | 145:206bf0d073c7 | 37 | return size; |
andrewboyson | 145:206bf0d073c7 | 38 | } |
andrewboyson | 145:206bf0d073c7 | 39 | void HttpAddData (const char* data, int length) |
andrewboyson | 145:206bf0d073c7 | 40 | { |
andrewboyson | 145:206bf0d073c7 | 41 | while (length > 0) |
andrewboyson | 145:206bf0d073c7 | 42 | { |
andrewboyson | 145:206bf0d073c7 | 43 | HttpAddChar(*data); |
andrewboyson | 145:206bf0d073c7 | 44 | data++; |
andrewboyson | 145:206bf0d073c7 | 45 | length--; |
andrewboyson | 145:206bf0d073c7 | 46 | } |
andrewboyson | 145:206bf0d073c7 | 47 | } |
andrewboyson | 145:206bf0d073c7 | 48 | void HttpAddStream(void (*startFunction)(void), int (*enumerateFunction)(void)) |
andrewboyson | 145:206bf0d073c7 | 49 | { |
andrewboyson | 145:206bf0d073c7 | 50 | startFunction(); |
andrewboyson | 145:206bf0d073c7 | 51 | while (true) |
andrewboyson | 145:206bf0d073c7 | 52 | { |
andrewboyson | 145:206bf0d073c7 | 53 | int c = enumerateFunction(); |
andrewboyson | 145:206bf0d073c7 | 54 | if (c == EOF) break; |
andrewboyson | 145:206bf0d073c7 | 55 | HttpAddChar(c); |
andrewboyson | 145:206bf0d073c7 | 56 | } |
andrewboyson | 145:206bf0d073c7 | 57 | } |
andrewboyson | 145:206bf0d073c7 | 58 | void HttpAddNibbleAsHex(int nibble) |
andrewboyson | 145:206bf0d073c7 | 59 | { |
andrewboyson | 145:206bf0d073c7 | 60 | nibble &= 0x0F; |
andrewboyson | 145:206bf0d073c7 | 61 | char c; |
andrewboyson | 145:206bf0d073c7 | 62 | if (nibble < 0x0A) c = nibble + '0'; |
andrewboyson | 145:206bf0d073c7 | 63 | else if (nibble < 0x10) c = nibble - 0xA + 'A'; |
andrewboyson | 145:206bf0d073c7 | 64 | else c = '0'; |
andrewboyson | 145:206bf0d073c7 | 65 | HttpAddChar(c); |
andrewboyson | 145:206bf0d073c7 | 66 | } |
andrewboyson | 145:206bf0d073c7 | 67 | void HttpAddByteAsHex(int value) |
andrewboyson | 145:206bf0d073c7 | 68 | { |
andrewboyson | 145:206bf0d073c7 | 69 | HttpAddNibbleAsHex(value >> 4); |
andrewboyson | 145:206bf0d073c7 | 70 | HttpAddNibbleAsHex(value >> 0); |
andrewboyson | 145:206bf0d073c7 | 71 | } |
andrewboyson | 145:206bf0d073c7 | 72 | void HttpAddInt12AsHex(int value) |
andrewboyson | 145:206bf0d073c7 | 73 | { |
andrewboyson | 145:206bf0d073c7 | 74 | HttpAddNibbleAsHex(value >> 8); |
andrewboyson | 145:206bf0d073c7 | 75 | HttpAddNibbleAsHex(value >> 4); |
andrewboyson | 145:206bf0d073c7 | 76 | HttpAddNibbleAsHex(value >> 0); |
andrewboyson | 145:206bf0d073c7 | 77 | } |
andrewboyson | 145:206bf0d073c7 | 78 | void HttpAddInt16AsHex(int value) |
andrewboyson | 145:206bf0d073c7 | 79 | { |
andrewboyson | 145:206bf0d073c7 | 80 | HttpAddNibbleAsHex(value >> 12); |
andrewboyson | 145:206bf0d073c7 | 81 | HttpAddNibbleAsHex(value >> 8); |
andrewboyson | 145:206bf0d073c7 | 82 | HttpAddNibbleAsHex(value >> 4); |
andrewboyson | 145:206bf0d073c7 | 83 | HttpAddNibbleAsHex(value >> 0); |
andrewboyson | 145:206bf0d073c7 | 84 | } |
andrewboyson | 145:206bf0d073c7 | 85 | void HttpAddInt32AsHex(int value) |
andrewboyson | 145:206bf0d073c7 | 86 | { |
andrewboyson | 145:206bf0d073c7 | 87 | HttpAddNibbleAsHex(value >> 28); |
andrewboyson | 145:206bf0d073c7 | 88 | HttpAddNibbleAsHex(value >> 24); |
andrewboyson | 145:206bf0d073c7 | 89 | HttpAddNibbleAsHex(value >> 20); |
andrewboyson | 145:206bf0d073c7 | 90 | HttpAddNibbleAsHex(value >> 16); |
andrewboyson | 145:206bf0d073c7 | 91 | HttpAddNibbleAsHex(value >> 12); |
andrewboyson | 145:206bf0d073c7 | 92 | HttpAddNibbleAsHex(value >> 8); |
andrewboyson | 145:206bf0d073c7 | 93 | HttpAddNibbleAsHex(value >> 4); |
andrewboyson | 145:206bf0d073c7 | 94 | HttpAddNibbleAsHex(value >> 0); |
andrewboyson | 145:206bf0d073c7 | 95 | } |
andrewboyson | 145:206bf0d073c7 | 96 | void HttpAddInt64AsHex(int64_t value) |
andrewboyson | 145:206bf0d073c7 | 97 | { |
andrewboyson | 145:206bf0d073c7 | 98 | HttpAddNibbleAsHex(value >> 60); |
andrewboyson | 145:206bf0d073c7 | 99 | HttpAddNibbleAsHex(value >> 56); |
andrewboyson | 145:206bf0d073c7 | 100 | HttpAddNibbleAsHex(value >> 52); |
andrewboyson | 145:206bf0d073c7 | 101 | HttpAddNibbleAsHex(value >> 48); |
andrewboyson | 145:206bf0d073c7 | 102 | HttpAddNibbleAsHex(value >> 44); |
andrewboyson | 145:206bf0d073c7 | 103 | HttpAddNibbleAsHex(value >> 40); |
andrewboyson | 145:206bf0d073c7 | 104 | HttpAddNibbleAsHex(value >> 36); |
andrewboyson | 145:206bf0d073c7 | 105 | HttpAddNibbleAsHex(value >> 32); |
andrewboyson | 145:206bf0d073c7 | 106 | HttpAddNibbleAsHex(value >> 28); |
andrewboyson | 145:206bf0d073c7 | 107 | HttpAddNibbleAsHex(value >> 24); |
andrewboyson | 145:206bf0d073c7 | 108 | HttpAddNibbleAsHex(value >> 20); |
andrewboyson | 145:206bf0d073c7 | 109 | HttpAddNibbleAsHex(value >> 16); |
andrewboyson | 145:206bf0d073c7 | 110 | HttpAddNibbleAsHex(value >> 12); |
andrewboyson | 145:206bf0d073c7 | 111 | HttpAddNibbleAsHex(value >> 8); |
andrewboyson | 145:206bf0d073c7 | 112 | HttpAddNibbleAsHex(value >> 4); |
andrewboyson | 145:206bf0d073c7 | 113 | HttpAddNibbleAsHex(value >> 0); |
andrewboyson | 145:206bf0d073c7 | 114 | } |
andrewboyson | 145:206bf0d073c7 | 115 | void HttpAddTm(struct tm* ptm) |
andrewboyson | 145:206bf0d073c7 | 116 | { |
andrewboyson | 145:206bf0d073c7 | 117 | HttpAddF("%d-%02d-%02d ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday); |
andrewboyson | 145:206bf0d073c7 | 118 | switch(ptm->tm_wday) |
andrewboyson | 145:206bf0d073c7 | 119 | { |
andrewboyson | 145:206bf0d073c7 | 120 | case 0: HttpAddText("Sun"); break; |
andrewboyson | 145:206bf0d073c7 | 121 | case 1: HttpAddText("Mon"); break; |
andrewboyson | 145:206bf0d073c7 | 122 | case 2: HttpAddText("Tue"); break; |
andrewboyson | 145:206bf0d073c7 | 123 | case 3: HttpAddText("Wed"); break; |
andrewboyson | 145:206bf0d073c7 | 124 | case 4: HttpAddText("Thu"); break; |
andrewboyson | 145:206bf0d073c7 | 125 | case 5: HttpAddText("Fri"); break; |
andrewboyson | 145:206bf0d073c7 | 126 | case 6: HttpAddText("Sat"); break; |
andrewboyson | 145:206bf0d073c7 | 127 | default: HttpAddText("???"); break; |
andrewboyson | 145:206bf0d073c7 | 128 | } |
andrewboyson | 145:206bf0d073c7 | 129 | HttpAddF(" %02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); |
andrewboyson | 145:206bf0d073c7 | 130 | if (ptm->tm_isdst > 0) HttpAddText(" BST"); |
andrewboyson | 145:206bf0d073c7 | 131 | else if (ptm->tm_isdst == 0) HttpAddText(" GMT"); |
andrewboyson | 145:206bf0d073c7 | 132 | else HttpAddText(" UTC"); |
andrewboyson | 145:206bf0d073c7 | 133 | } |