diff --git a/myHTTP2022-2010.vcxproj b/myHTTP2022-2010.vcxproj
index 53866d752..09c63c30e 100644
--- a/myHTTP2022-2010.vcxproj
+++ b/myHTTP2022-2010.vcxproj
@@ -78,6 +78,8 @@
true
+
+
diff --git a/src/httpserver/http_tcp_server.c b/src/httpserver/http_tcp_server.c
index e7e36232e..43390add7 100644
--- a/src/httpserver/http_tcp_server.c
+++ b/src/httpserver/http_tcp_server.c
@@ -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 );
diff --git a/src/httpserver/new_http.c b/src/httpserver/new_http.c
index bf82e79a6..73fdede29 100644
--- a/src/httpserver/new_http.c
+++ b/src/httpserver/new_http.c
@@ -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;
diff --git a/src/httpserver/new_http.h b/src/httpserver/new_http.h
index 558495081..739a6dcdd 100644
--- a/src/httpserver/new_http.h
+++ b/src/httpserver/new_http.h
@@ -29,6 +29,7 @@ typedef struct http_request_tag {
// used to respond
char *reply;
+ int replylen;
int replymaxlen;
int fd;
} http_request_t;
diff --git a/src/httpserver/rest_interface.c b/src/httpserver/rest_interface.c
index 727fbc011..aecb6ebc4 100644
--- a/src/httpserver/rest_interface.c
+++ b/src/httpserver/rest_interface.c
@@ -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\">"
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;
}
diff --git a/src/win_main.c b/src/win_main.c
index be0f880bf..6d3b521d0 100644
--- a/src/win_main.c
+++ b/src/win_main.c
@@ -70,6 +70,7 @@ int __cdecl main(void)
PIN_SetPinChannelForPinIndex(2,2);
PIN_SetPinRoleForPinIndex(2,IOR_PWM);
+ init_rest();
// Initialize Winsock
iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
@@ -146,7 +147,10 @@ int __cdecl main(void)
request.fd = ClientSocket;
request.received = recvbuf;
request.receivedLen = iResult;
+ outbuf[0] = '\0';
request.reply = outbuf;
+ request.replylen = 0;
+
request.replymaxlen = DEFAULT_BUFLEN;
printf("Bytes received: %d \n", iResult);