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@152:80a3840fc9f8, 2019-07-25 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu Jul 25 21:11:37 2019 +0000
- Revision:
- 152:80a3840fc9f8
- Parent:
- 146:0fc66d610fd6
- Child:
- 156:be12b8fd5b21
Pulled a lot of the crypto stuff out into a separate library.
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 | 146:0fc66d610fd6 | 5 | #include "httpshim.h" |
andrewboyson | 145:206bf0d073c7 | 6 | |
andrewboyson | 146:0fc66d610fd6 | 7 | bool HttpBufFilled(void) |
andrewboyson | 146:0fc66d610fd6 | 8 | { |
andrewboyson | 146:0fc66d610fd6 | 9 | return HttpShimBufFilled(); |
andrewboyson | 146:0fc66d610fd6 | 10 | } |
andrewboyson | 146:0fc66d610fd6 | 11 | |
andrewboyson | 146:0fc66d610fd6 | 12 | void HttpAddChar(char c) |
andrewboyson | 146:0fc66d610fd6 | 13 | { |
andrewboyson | 146:0fc66d610fd6 | 14 | HttpShimAddChar(c); |
andrewboyson | 146:0fc66d610fd6 | 15 | } |
andrewboyson | 145:206bf0d073c7 | 16 | void HttpFillChar (char c, int length) |
andrewboyson | 145:206bf0d073c7 | 17 | { |
andrewboyson | 145:206bf0d073c7 | 18 | while (length > 0) |
andrewboyson | 145:206bf0d073c7 | 19 | { |
andrewboyson | 146:0fc66d610fd6 | 20 | HttpShimAddChar(c); |
andrewboyson | 145:206bf0d073c7 | 21 | length--; |
andrewboyson | 145:206bf0d073c7 | 22 | } |
andrewboyson | 145:206bf0d073c7 | 23 | } |
andrewboyson | 145:206bf0d073c7 | 24 | int HttpAddText (const char* text) |
andrewboyson | 145:206bf0d073c7 | 25 | { |
andrewboyson | 145:206bf0d073c7 | 26 | const char* start = text; |
andrewboyson | 145:206bf0d073c7 | 27 | while (*text) |
andrewboyson | 145:206bf0d073c7 | 28 | { |
andrewboyson | 146:0fc66d610fd6 | 29 | HttpShimAddChar(*text); |
andrewboyson | 145:206bf0d073c7 | 30 | text++; |
andrewboyson | 145:206bf0d073c7 | 31 | } |
andrewboyson | 145:206bf0d073c7 | 32 | return text - start; |
andrewboyson | 145:206bf0d073c7 | 33 | } |
andrewboyson | 145:206bf0d073c7 | 34 | int HttpAddV (char *fmt, va_list argptr) |
andrewboyson | 145:206bf0d073c7 | 35 | { |
andrewboyson | 145:206bf0d073c7 | 36 | int size = vsnprintf(NULL, 0, fmt, argptr); //Find the size required |
andrewboyson | 145:206bf0d073c7 | 37 | char text[size + 1]; //Allocate enough memory for the size required with an extra byte for the terminating null |
andrewboyson | 145:206bf0d073c7 | 38 | vsprintf(text, fmt, argptr); //Fill the new buffer |
andrewboyson | 145:206bf0d073c7 | 39 | return HttpAddText(text); //Add the text |
andrewboyson | 145:206bf0d073c7 | 40 | } |
andrewboyson | 145:206bf0d073c7 | 41 | int HttpAddF (char *fmt, ...) |
andrewboyson | 145:206bf0d073c7 | 42 | { |
andrewboyson | 145:206bf0d073c7 | 43 | va_list argptr; |
andrewboyson | 145:206bf0d073c7 | 44 | va_start(argptr, fmt); |
andrewboyson | 145:206bf0d073c7 | 45 | int size = HttpAddV(fmt, argptr); |
andrewboyson | 145:206bf0d073c7 | 46 | va_end(argptr); |
andrewboyson | 145:206bf0d073c7 | 47 | return size; |
andrewboyson | 145:206bf0d073c7 | 48 | } |
andrewboyson | 145:206bf0d073c7 | 49 | void HttpAddData (const char* data, int length) |
andrewboyson | 145:206bf0d073c7 | 50 | { |
andrewboyson | 145:206bf0d073c7 | 51 | while (length > 0) |
andrewboyson | 145:206bf0d073c7 | 52 | { |
andrewboyson | 146:0fc66d610fd6 | 53 | HttpShimAddChar(*data); |
andrewboyson | 145:206bf0d073c7 | 54 | data++; |
andrewboyson | 145:206bf0d073c7 | 55 | length--; |
andrewboyson | 145:206bf0d073c7 | 56 | } |
andrewboyson | 145:206bf0d073c7 | 57 | } |
andrewboyson | 145:206bf0d073c7 | 58 | void HttpAddStream(void (*startFunction)(void), int (*enumerateFunction)(void)) |
andrewboyson | 145:206bf0d073c7 | 59 | { |
andrewboyson | 145:206bf0d073c7 | 60 | startFunction(); |
andrewboyson | 145:206bf0d073c7 | 61 | while (true) |
andrewboyson | 145:206bf0d073c7 | 62 | { |
andrewboyson | 145:206bf0d073c7 | 63 | int c = enumerateFunction(); |
andrewboyson | 145:206bf0d073c7 | 64 | if (c == EOF) break; |
andrewboyson | 146:0fc66d610fd6 | 65 | HttpShimAddChar(c); |
andrewboyson | 145:206bf0d073c7 | 66 | } |
andrewboyson | 145:206bf0d073c7 | 67 | } |
andrewboyson | 145:206bf0d073c7 | 68 | void HttpAddNibbleAsHex(int nibble) |
andrewboyson | 145:206bf0d073c7 | 69 | { |
andrewboyson | 145:206bf0d073c7 | 70 | nibble &= 0x0F; |
andrewboyson | 145:206bf0d073c7 | 71 | char c; |
andrewboyson | 145:206bf0d073c7 | 72 | if (nibble < 0x0A) c = nibble + '0'; |
andrewboyson | 145:206bf0d073c7 | 73 | else if (nibble < 0x10) c = nibble - 0xA + 'A'; |
andrewboyson | 145:206bf0d073c7 | 74 | else c = '0'; |
andrewboyson | 146:0fc66d610fd6 | 75 | HttpShimAddChar(c); |
andrewboyson | 145:206bf0d073c7 | 76 | } |
andrewboyson | 145:206bf0d073c7 | 77 | void HttpAddByteAsHex(int value) |
andrewboyson | 145:206bf0d073c7 | 78 | { |
andrewboyson | 145:206bf0d073c7 | 79 | HttpAddNibbleAsHex(value >> 4); |
andrewboyson | 145:206bf0d073c7 | 80 | HttpAddNibbleAsHex(value >> 0); |
andrewboyson | 145:206bf0d073c7 | 81 | } |
andrewboyson | 145:206bf0d073c7 | 82 | void HttpAddInt12AsHex(int value) |
andrewboyson | 145:206bf0d073c7 | 83 | { |
andrewboyson | 145:206bf0d073c7 | 84 | HttpAddNibbleAsHex(value >> 8); |
andrewboyson | 145:206bf0d073c7 | 85 | HttpAddNibbleAsHex(value >> 4); |
andrewboyson | 145:206bf0d073c7 | 86 | HttpAddNibbleAsHex(value >> 0); |
andrewboyson | 145:206bf0d073c7 | 87 | } |
andrewboyson | 145:206bf0d073c7 | 88 | void HttpAddInt16AsHex(int value) |
andrewboyson | 145:206bf0d073c7 | 89 | { |
andrewboyson | 145:206bf0d073c7 | 90 | HttpAddNibbleAsHex(value >> 12); |
andrewboyson | 145:206bf0d073c7 | 91 | HttpAddNibbleAsHex(value >> 8); |
andrewboyson | 145:206bf0d073c7 | 92 | HttpAddNibbleAsHex(value >> 4); |
andrewboyson | 145:206bf0d073c7 | 93 | HttpAddNibbleAsHex(value >> 0); |
andrewboyson | 145:206bf0d073c7 | 94 | } |
andrewboyson | 145:206bf0d073c7 | 95 | void HttpAddInt32AsHex(int value) |
andrewboyson | 145:206bf0d073c7 | 96 | { |
andrewboyson | 145:206bf0d073c7 | 97 | HttpAddNibbleAsHex(value >> 28); |
andrewboyson | 145:206bf0d073c7 | 98 | HttpAddNibbleAsHex(value >> 24); |
andrewboyson | 145:206bf0d073c7 | 99 | HttpAddNibbleAsHex(value >> 20); |
andrewboyson | 145:206bf0d073c7 | 100 | HttpAddNibbleAsHex(value >> 16); |
andrewboyson | 145:206bf0d073c7 | 101 | HttpAddNibbleAsHex(value >> 12); |
andrewboyson | 145:206bf0d073c7 | 102 | HttpAddNibbleAsHex(value >> 8); |
andrewboyson | 145:206bf0d073c7 | 103 | HttpAddNibbleAsHex(value >> 4); |
andrewboyson | 145:206bf0d073c7 | 104 | HttpAddNibbleAsHex(value >> 0); |
andrewboyson | 145:206bf0d073c7 | 105 | } |
andrewboyson | 145:206bf0d073c7 | 106 | void HttpAddInt64AsHex(int64_t value) |
andrewboyson | 145:206bf0d073c7 | 107 | { |
andrewboyson | 145:206bf0d073c7 | 108 | HttpAddNibbleAsHex(value >> 60); |
andrewboyson | 145:206bf0d073c7 | 109 | HttpAddNibbleAsHex(value >> 56); |
andrewboyson | 145:206bf0d073c7 | 110 | HttpAddNibbleAsHex(value >> 52); |
andrewboyson | 145:206bf0d073c7 | 111 | HttpAddNibbleAsHex(value >> 48); |
andrewboyson | 145:206bf0d073c7 | 112 | HttpAddNibbleAsHex(value >> 44); |
andrewboyson | 145:206bf0d073c7 | 113 | HttpAddNibbleAsHex(value >> 40); |
andrewboyson | 145:206bf0d073c7 | 114 | HttpAddNibbleAsHex(value >> 36); |
andrewboyson | 145:206bf0d073c7 | 115 | HttpAddNibbleAsHex(value >> 32); |
andrewboyson | 145:206bf0d073c7 | 116 | HttpAddNibbleAsHex(value >> 28); |
andrewboyson | 145:206bf0d073c7 | 117 | HttpAddNibbleAsHex(value >> 24); |
andrewboyson | 145:206bf0d073c7 | 118 | HttpAddNibbleAsHex(value >> 20); |
andrewboyson | 145:206bf0d073c7 | 119 | HttpAddNibbleAsHex(value >> 16); |
andrewboyson | 145:206bf0d073c7 | 120 | HttpAddNibbleAsHex(value >> 12); |
andrewboyson | 145:206bf0d073c7 | 121 | HttpAddNibbleAsHex(value >> 8); |
andrewboyson | 145:206bf0d073c7 | 122 | HttpAddNibbleAsHex(value >> 4); |
andrewboyson | 145:206bf0d073c7 | 123 | HttpAddNibbleAsHex(value >> 0); |
andrewboyson | 145:206bf0d073c7 | 124 | } |
andrewboyson | 152:80a3840fc9f8 | 125 | void HttpAddBytesAsHex(const char* value, int size) |
andrewboyson | 152:80a3840fc9f8 | 126 | { |
andrewboyson | 152:80a3840fc9f8 | 127 | int i = 0; |
andrewboyson | 152:80a3840fc9f8 | 128 | while(true) |
andrewboyson | 152:80a3840fc9f8 | 129 | { |
andrewboyson | 152:80a3840fc9f8 | 130 | HttpAddByteAsHex(value[i]); |
andrewboyson | 152:80a3840fc9f8 | 131 | i++; |
andrewboyson | 152:80a3840fc9f8 | 132 | if (i >= size) break; |
andrewboyson | 152:80a3840fc9f8 | 133 | if (i % 16 == 0) HttpAddText("\r\n"); |
andrewboyson | 152:80a3840fc9f8 | 134 | else HttpAddChar(' '); |
andrewboyson | 152:80a3840fc9f8 | 135 | } |
andrewboyson | 152:80a3840fc9f8 | 136 | } |
andrewboyson | 152:80a3840fc9f8 | 137 | void HttpAddBytesAsHexRev(const char* value, int size) |
andrewboyson | 152:80a3840fc9f8 | 138 | { |
andrewboyson | 152:80a3840fc9f8 | 139 | int i = 0; |
andrewboyson | 152:80a3840fc9f8 | 140 | while(true) |
andrewboyson | 152:80a3840fc9f8 | 141 | { |
andrewboyson | 152:80a3840fc9f8 | 142 | HttpAddByteAsHex(value[size - i - 1]); |
andrewboyson | 152:80a3840fc9f8 | 143 | i++; |
andrewboyson | 152:80a3840fc9f8 | 144 | if (i >= size) break; |
andrewboyson | 152:80a3840fc9f8 | 145 | if (i % 16 == 0) HttpAddText("\r\n"); |
andrewboyson | 152:80a3840fc9f8 | 146 | else HttpAddChar(' '); |
andrewboyson | 152:80a3840fc9f8 | 147 | } |
andrewboyson | 152:80a3840fc9f8 | 148 | } |
andrewboyson | 145:206bf0d073c7 | 149 | void HttpAddTm(struct tm* ptm) |
andrewboyson | 145:206bf0d073c7 | 150 | { |
andrewboyson | 145:206bf0d073c7 | 151 | HttpAddF("%d-%02d-%02d ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday); |
andrewboyson | 145:206bf0d073c7 | 152 | switch(ptm->tm_wday) |
andrewboyson | 145:206bf0d073c7 | 153 | { |
andrewboyson | 145:206bf0d073c7 | 154 | case 0: HttpAddText("Sun"); break; |
andrewboyson | 145:206bf0d073c7 | 155 | case 1: HttpAddText("Mon"); break; |
andrewboyson | 145:206bf0d073c7 | 156 | case 2: HttpAddText("Tue"); break; |
andrewboyson | 145:206bf0d073c7 | 157 | case 3: HttpAddText("Wed"); break; |
andrewboyson | 145:206bf0d073c7 | 158 | case 4: HttpAddText("Thu"); break; |
andrewboyson | 145:206bf0d073c7 | 159 | case 5: HttpAddText("Fri"); break; |
andrewboyson | 145:206bf0d073c7 | 160 | case 6: HttpAddText("Sat"); break; |
andrewboyson | 145:206bf0d073c7 | 161 | default: HttpAddText("???"); break; |
andrewboyson | 145:206bf0d073c7 | 162 | } |
andrewboyson | 145:206bf0d073c7 | 163 | HttpAddF(" %02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec); |
andrewboyson | 145:206bf0d073c7 | 164 | if (ptm->tm_isdst > 0) HttpAddText(" BST"); |
andrewboyson | 145:206bf0d073c7 | 165 | else if (ptm->tm_isdst == 0) HttpAddText(" GMT"); |
andrewboyson | 145:206bf0d073c7 | 166 | else HttpAddText(" UTC"); |
andrewboyson | 145:206bf0d073c7 | 167 | } |