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

Committer:
andrewboyson
Date:
Wed May 15 15:33:15 2019 +0000
Revision:
146:0fc66d610fd6
Parent:
145:206bf0d073c7
Child:
152:80a3840fc9f8
Tidied the http shim

Who changed what in which revision?

UserRevisionLine numberNew 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 145:206bf0d073c7 125 void HttpAddTm(struct tm* ptm)
andrewboyson 145:206bf0d073c7 126 {
andrewboyson 145:206bf0d073c7 127 HttpAddF("%d-%02d-%02d ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
andrewboyson 145:206bf0d073c7 128 switch(ptm->tm_wday)
andrewboyson 145:206bf0d073c7 129 {
andrewboyson 145:206bf0d073c7 130 case 0: HttpAddText("Sun"); break;
andrewboyson 145:206bf0d073c7 131 case 1: HttpAddText("Mon"); break;
andrewboyson 145:206bf0d073c7 132 case 2: HttpAddText("Tue"); break;
andrewboyson 145:206bf0d073c7 133 case 3: HttpAddText("Wed"); break;
andrewboyson 145:206bf0d073c7 134 case 4: HttpAddText("Thu"); break;
andrewboyson 145:206bf0d073c7 135 case 5: HttpAddText("Fri"); break;
andrewboyson 145:206bf0d073c7 136 case 6: HttpAddText("Sat"); break;
andrewboyson 145:206bf0d073c7 137 default: HttpAddText("???"); break;
andrewboyson 145:206bf0d073c7 138 }
andrewboyson 145:206bf0d073c7 139 HttpAddF(" %02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
andrewboyson 145:206bf0d073c7 140 if (ptm->tm_isdst > 0) HttpAddText(" BST");
andrewboyson 145:206bf0d073c7 141 else if (ptm->tm_isdst == 0) HttpAddText(" GMT");
andrewboyson 145:206bf0d073c7 142 else HttpAddText(" UTC");
andrewboyson 145:206bf0d073c7 143 }