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

Committer:
andrewboyson
Date:
2019-05-15
Revision:
146:0fc66d610fd6
Parent:
145:206bf0d073c7
Child:
152:80a3840fc9f8

File content as of revision 146:0fc66d610fd6:

#include <time.h>
#include <stdio.h>

#include "http.h"
#include "httpshim.h"

bool HttpBufFilled(void)
{
    return HttpShimBufFilled();
}

void HttpAddChar(char c)
{
    HttpShimAddChar(c);
}
void HttpFillChar (char c, int length)
{
    while (length > 0)
    {
        HttpShimAddChar(c);
        length--;
    }
}
int  HttpAddText  (const char* text)
{
    const char* start = text;
    while (*text)
    {
        HttpShimAddChar(*text);
        text++;
    }
    return text - start;
}
int  HttpAddV     (char *fmt, va_list argptr)
{
    int size  = vsnprintf(NULL, 0, fmt, argptr); //Find the size required
    char text[size + 1];                         //Allocate enough memory for the size required with an extra byte for the terminating null
    vsprintf(text, fmt, argptr);                 //Fill the new buffer
    return HttpAddText(text);                    //Add the text
}
int  HttpAddF     (char *fmt, ...)
{
    va_list argptr;
    va_start(argptr, fmt);
    int size = HttpAddV(fmt, argptr);
    va_end(argptr);
    return size;
}
void HttpAddData  (const char* data, int length)
{
    while (length > 0)
    {
        HttpShimAddChar(*data);
        data++;
        length--;
    }
}
void HttpAddStream(void (*startFunction)(void), int (*enumerateFunction)(void))
{
    startFunction();
    while (true)
    {
        int c = enumerateFunction();
        if (c == EOF) break;
        HttpShimAddChar(c);
    }
}
void HttpAddNibbleAsHex(int nibble)
{
    nibble &= 0x0F;
    char c;
    if      (nibble < 0x0A) c = nibble + '0';
    else if (nibble < 0x10) c = nibble - 0xA + 'A';
    else                    c = '0';
    HttpShimAddChar(c);
}
void HttpAddByteAsHex(int value)
{
    HttpAddNibbleAsHex(value >> 4);
    HttpAddNibbleAsHex(value >> 0);
}
void HttpAddInt12AsHex(int value)
{   
    HttpAddNibbleAsHex(value >> 8);
    HttpAddNibbleAsHex(value >> 4);
    HttpAddNibbleAsHex(value >> 0);
}
void HttpAddInt16AsHex(int value)
{   
    HttpAddNibbleAsHex(value >> 12);
    HttpAddNibbleAsHex(value >>  8);
    HttpAddNibbleAsHex(value >>  4);
    HttpAddNibbleAsHex(value >>  0);
}
void HttpAddInt32AsHex(int value)
{   
    HttpAddNibbleAsHex(value >> 28);
    HttpAddNibbleAsHex(value >> 24);
    HttpAddNibbleAsHex(value >> 20);
    HttpAddNibbleAsHex(value >> 16);
    HttpAddNibbleAsHex(value >> 12);
    HttpAddNibbleAsHex(value >>  8);
    HttpAddNibbleAsHex(value >>  4);
    HttpAddNibbleAsHex(value >>  0);
}
void HttpAddInt64AsHex(int64_t value)
{   
    HttpAddNibbleAsHex(value >> 60);
    HttpAddNibbleAsHex(value >> 56);
    HttpAddNibbleAsHex(value >> 52);
    HttpAddNibbleAsHex(value >> 48);
    HttpAddNibbleAsHex(value >> 44);
    HttpAddNibbleAsHex(value >> 40);
    HttpAddNibbleAsHex(value >> 36);
    HttpAddNibbleAsHex(value >> 32);
    HttpAddNibbleAsHex(value >> 28);
    HttpAddNibbleAsHex(value >> 24);
    HttpAddNibbleAsHex(value >> 20);
    HttpAddNibbleAsHex(value >> 16);
    HttpAddNibbleAsHex(value >> 12);
    HttpAddNibbleAsHex(value >>  8);
    HttpAddNibbleAsHex(value >>  4);
    HttpAddNibbleAsHex(value >>  0);
}
void HttpAddTm(struct tm* ptm)
{
    HttpAddF("%d-%02d-%02d ", ptm->tm_year + 1900, ptm->tm_mon + 1, ptm->tm_mday);
    switch(ptm->tm_wday)
    {
        case  0: HttpAddText("Sun"); break;
        case  1: HttpAddText("Mon"); break;
        case  2: HttpAddText("Tue"); break;
        case  3: HttpAddText("Wed"); break;
        case  4: HttpAddText("Thu"); break;
        case  5: HttpAddText("Fri"); break;
        case  6: HttpAddText("Sat"); break;
        default: HttpAddText("???"); break;
    }
    HttpAddF(" %02d:%02d:%02d", ptm->tm_hour, ptm->tm_min, ptm->tm_sec);
    if      (ptm->tm_isdst  > 0) HttpAddText(" BST");
    else if (ptm->tm_isdst == 0) HttpAddText(" GMT");
    else                         HttpAddText(" UTC");
}