working json, plus some bugfixes

This commit is contained in:
btsimonh
2022-02-08 18:45:31 +00:00
parent 4174b08997
commit 62fb793019
6 changed files with 54 additions and 23 deletions

View File

@ -58,9 +58,12 @@ static void tcp_client_thread( beken_thread_arg_t arg )
request.fd = fd;
request.received = buf;
request.receivedLen = recv( fd, request.received, 1024, 0 );
request.received[request.receivedLen] = 0;
request.reply = reply;
request.replylen = 0;
reply[0] = '\0';
request.replymaxlen = replyBufferSize - 1;
if ( request.receivedLen <= 0 )
@ -71,7 +74,6 @@ static void tcp_client_thread( beken_thread_arg_t arg )
//addLog( "TCP received string %s\n",buf );
// returns length to be sent if any
request.received[request.receivedLen] = 0;
int lenret = HTTP_ProcessPacket(&request);
if (lenret > 0){
addLog( "TCP sending reply len %i\n",lenret );

View File

@ -209,6 +209,7 @@ void http_copyCarg(const char *atin, char *to, int maxSize) {
*to = 0;
}
bool http_getArg(const char *base, const char *name, char *o, int maxSize) {
*o = '\0';
while(*base != '?') {
if(*base == 0)
return 0;
@ -313,31 +314,44 @@ void HTTP_AddBuildFooter(http_request_t *request) {
// add some more output safely, sending if necessary.
// call with str == NULL to force send.
int poststr(http_request_t *request, const char *str){
// call with str == NULL to force send. - can be binary.
// supply length
int postany(http_request_t *request, const char *str, int len){
int currentlen;
int addlen;
int addlen = len;
if (NULL == str){
send(request->fd, request->reply, strlen(request->reply), 0);
send(request->fd, request->reply, request->replylen, 0);
request->reply[0] = 0;
request->replylen = 0;
return 0;
}
currentlen = strlen(request->reply);
addlen = strlen(str);
currentlen = request->replylen;
if (currentlen + addlen >= request->replymaxlen){
send(request->fd, request->reply, strlen(request->reply), 0);
send(request->fd, request->reply, request->replylen, 0);
request->reply[0] = 0;
request->replylen = 0;
currentlen = 0;
}
if (addlen > request->replymaxlen){
printf("won't fit");
} else {
strcat(request->reply, str );
memcpy( request->reply+request->replylen, str, addlen );
request->replylen += addlen;
}
return (currentlen + addlen);
}
// add some more output safely, sending if necessary.
// call with str == NULL to force send.
int poststr(http_request_t *request, const char *str){
if (str == NULL){
return postany(request, NULL, 0);
}
return postany(request, str, strlen(str));
}
int HTTP_ProcessPacket(http_request_t *request) {
int i, j;
char tmpA[128];
@ -399,7 +413,6 @@ int HTTP_ProcessPacket(http_request_t *request) {
printf("invalid request\n");
return 0;
}
p++;
// i.e. not received
request->contentLength = -1;
headers = p;

View File

@ -29,6 +29,7 @@ typedef struct http_request_tag {
// used to respond
char *reply;
int replylen;
int replymaxlen;
int fd;
} http_request_t;

View File

@ -1,7 +1,7 @@
#include "../new_common.h"
#include "../logging/logging.h"
#include "../httpserver/new_http.h"
#include "str_pub.h"
//#include "str_pub.h"
#include "../new_pins.h"
#include "../jsmn/jsmn_h.h"
@ -37,10 +37,10 @@ const char * apppage4 = "/startup.js\"></script>"
static int http_rest_app(http_request_t *request){
http_setup(request, httpMimeTypeHTML);
//char *webhost = "http://raspberrypi:1880";//CFG_GetWebRoot();
char *webhost = "https://btsimonh.github.io/testwebpages/";
char *ourip = "192.168.1.176"; //CFG_GetOurIP();
http_setup(request, httpMimeTypeHTML);
if (webhost && ourip){
poststr(request, apppage1);
poststr(request, webhost);
@ -143,19 +143,26 @@ static int http_rest_post_pins(http_request_t *request){
char tmp[64];
//https://github.com/zserge/jsmn/blob/master/example/simple.c
jsmn_parser p;
jsmntok_t t[128]; /* We expect no more than 128 tokens */
//jsmn_parser p;
jsmn_parser *p = malloc(sizeof(jsmn_parser));
//jsmntok_t t[128]; /* We expect no more than 128 tokens */
#define TOKEN_COUNT 128
jsmntok_t *t = malloc(sizeof(jsmntok_t)*TOKEN_COUNT);
char *json_str = request->bodystart;
int json_len = strlen(json_str);
http_setup(request, httpMimeTypeText);
jsmn_init(&p);
r = jsmn_parse(&p, json_str, json_len, t,
sizeof(t) / sizeof(t[0]));
http_setup(request, httpMimeTypeText);
memset(p, 0, sizeof(jsmn_parser));
memset(t, 0, sizeof(jsmntok_t)*128);
jsmn_init(p);
r = jsmn_parse(p, json_str, json_len, t, TOKEN_COUNT);
if (r < 0) {
sprintf(tmp,"Failed to parse JSON: %d\n", r);
poststr(request, tmp);
poststr(request, NULL);
free(p);
free(t);
return 0;
}
@ -164,14 +171,14 @@ static int http_rest_post_pins(http_request_t *request){
sprintf(tmp,"Object expected\n");
poststr(request, tmp);
poststr(request, NULL);
free(p);
free(t);
return 0;
}
sprintf(tmp,"parsed JSON: %s\n", json_str);
poststr(request, tmp);
poststr(request, NULL);
return 0;
//sprintf(tmp,"parsed JSON: %s\n", json_str);
//poststr(request, tmp);
//poststr(request, NULL);
/* Loop over all keys of the root object */
for (i = 1; i < r; i++) {
@ -209,5 +216,7 @@ static int http_rest_post_pins(http_request_t *request){
}
poststr(request, NULL);
free(p);
free(t);
return 0;
}