This repository has been archived by the owner on Sep 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmicrohttpclient.c
62 lines (58 loc) · 2.04 KB
/
microhttpclient.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Copyright (C) 2017, Denys Fedoryshchenko
* Contact: <[email protected]>
* Licensed under the GPLv2
* <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>
*
* Extremely minimalistic handling of tcp inbound data to parse basic HTTP/1.0
* Just after request sent, do something like this:
* .. Establishing TCP, sending GET /url HTTP/1.0\r\nHeaders\r\n\r\n ...
* do {
* ret = recv(s, recv_buf, UPG_BUF_SZ, MSG_DONTWAIT);
* if (ret > 0)
* parse_http(&state, recv_buf, &ret, &fwupcb);
* }
* Where fwupcb will be callback that will receive "body" data
* WARNING! I dont do supplied params check, so callback must be not NULL, size > 0, etc
* Still experimental.
*/
#include "esp_common.h"
/* Upgrade process state */
#define STATUSLINE 0
#define NEWLINE 1
#define READING 2
#define BODY 3
#define ERROR 255
void parse_http(uint8_t *state, char *buf, int *size, void *callback) {
int i = 0;
void (*ptrFunc)(char*,int);
ptrFunc=callback;
/* We are on state of reading body, directly do callback */
if (*state == BODY) {
ptrFunc(buf, *size);
return;
}
for (i=0; i<*size; i++) {
switch(buf[i]) {
case '\n':
*state = NEWLINE;
break;
case '\r':
if (*state == NEWLINE) {
// Still we might have some body left in chunk
int offset = i+2;
*state = BODY;
if (offset < *size) {
ptrFunc(&buf[offset], *size-offset);
*size -= offset;
}
return;
}
break;
default:
if (*state == NEWLINE)
*state = READING;
break;
}
}
}