KPN Things Device SDK
thingsml_http.h
Go to the documentation of this file.
1/* _ __ ____ _ _
2 * | |/ / | _ \ | \ | |
3 * | ' / | |_) | | \| |
4 * | . \ | __/ | |\ |
5 * |_|\_\ |_| |_| \_|
6 *
7 * (c) 2020 KPN
8 * License: MIT License.
9 * Author: Joseph Verburg, Jan Bogaerts
10 *
11 *
12 * ThingsML helper for http requests
13 *
14 */
15#ifndef THINGSML_HTTP
16#define THINGSML_HTTP
17
18#include "thingsml.h"
19#include <string.h>
20#include <stdio.h>
21#include "util/sha256.h"
22
23namespace ThingsML {
35 size_t httpPost(char buffer[], int bufferSize, const char key[], const char host[], const char path[], SenMLBasePack &body);
36
43 int getHttpBodyStart(const char response[], int responseLength);
44
45
56 int strstr_n(const char data[], int dataLength, const char needle[], int needleLength);
57
58 // size_t httpPostCbor(char buffer[], int bufferSize, char key[], char host[], char path[], SenMLBasePack &body) {
59 // int keySize = strlen(key);
60 // int hostLen = strlen(host);
61 // int pathLen = strlen(path);
62 // char * buff = buffer;
63 // char * token = NULL;
64 // if (bufferSize < (hostLen + pathLen + 127)) {
65 // return -1;
66 // }
67 // int bodyLen = body.toCbor(buffer, bufferSize, SENML_HEX) * 2; // Hex duplicates
68 // char contentLength[5] = {0};
69 // int contentLengthLen = sprintf(contentLength, "%u", bodyLen);
70 // int total = hostLen + pathLen + bodyLen + contentLengthLen + 127;
71 // if (bufferSize < total) {
72 // return -1;
73 // }
74
75 // // POST /path HTTP/1.1
76 // memcpy(buff, "POST ", 5);
77 // buff += 5;
78 // memcpy(buff, path, pathLen);
79 // buff += pathLen;
80 // memcpy(buff, " HTTP/1.1\n", 10);
81 // buff += 10;
82
83 // // Host: domain.com
84 // memcpy(buff, "Host: ", 6);
85 // buff += 6;
86 // memcpy(buff, host, hostLen);
87 // buff += hostLen;
88 // memcpy(buff, "\n", 1);
89 // buff += 1;
90
91 // // Things-Message-Token: asdasdsad
92 // memcpy(buff, "Things-Message-Token: ", 22);
93 // buff += 22;
94 // token = buff;
95 // buff += 64; // Make space for the future token
96 // memcpy(buff, "\n", 1);
97 // buff += 1;
98
99 // // Content-Length: 1337
100 // memcpy(buff, "Content-Length: ", 16);
101 // buff += 16;
102 // memcpy(buff, contentLength, contentLengthLen);
103 // buff += contentLengthLen;
104 // memcpy(buff, "\n\n", 2);
105 // buff += 2;
106
107 // body.toCbor(buff, bufferSize - (buff - buffer), SENML_HEX);
108
109 // Sha256 hash;
110 // hash.init();
111 // hash.write(buff, bodyLen);
112 // hash.write(key, keySize);
113 // hash.result(token, 64);
114
115 // return total;
116 // }
117}
118
119#endif // !THINGSML_HTTP
Definition: senml_basepack.h:24
Definition: thingsml_http.cpp:4
size_t httpPost(char buffer[], int bufferSize, const char key[], const char host[], const char path[], SenMLBasePack &body)
Definition: thingsml_http.cpp:5
int strstr_n(const char data[], int dataLength, const char needle[], int needleLength)
Definition: thingsml_http.cpp:85
int getHttpBodyStart(const char response[], int responseLength)
Definition: thingsml_http.cpp:70