http server return bugfix

This commit is contained in:
btsimonh
2022-02-06 20:25:19 +00:00
parent b98c074be9
commit 97142ea969
3 changed files with 44 additions and 9 deletions

View File

@ -27,11 +27,19 @@ void start_tcp_http()
}
}
int sendfn(int fd, char * data, int len){
if (fd){
return send( fd, data, len, 0 );
}
return -1;
}
static void tcp_client_thread( beken_thread_arg_t arg )
{
OSStatus err = kNoErr;
int fd = (int) arg;
int len = 0;
int lenrx = 0;
//fd_set readfds, errfds, readfds2;
char *buf = NULL;
char *reply = NULL;
@ -47,9 +55,9 @@ static void tcp_client_thread( beken_thread_arg_t arg )
while ( 1 )
{
len = recv( fd, buf, 1024, 0 );
lenrx = recv( fd, buf, 1024, 0 );
if ( len <= 0 )
if ( lenrx <= 0 )
{
os_printf( "TCP Client is disconnected, fd: %d", fd );
goto exit;
@ -57,9 +65,16 @@ static void tcp_client_thread( beken_thread_arg_t arg )
//addLog( "TCP received string %s\n",buf );
// returns length to be sent if any
len = HTTP_ProcessPacket(buf, reply, replyBufferSize);
addLog( "TCP sending reply len %i\n",len );
len = send( fd, reply, len, 0 );
buf[lenrx] = 0;
int lenret = HTTP_ProcessPacket(buf, reply, replyBufferSize, sendfn, fd);
addLog( "TCP sending reply len %i\n",lenret );
while(lenret){
int len = lenret;
if (len > 1024) len = 1024;
send( fd, reply, len, 0 );
reply += len;
lenret -= len;
}
rtos_delay_milliseconds(10);
close(fd);

View File

@ -210,7 +210,8 @@ const char *htmlPinRoleNames[] = {
"LED",
"LED_n",
"PWM",
"e",
"Wifi LED",
"Wifi LED_n",
"e",
"e",
};
@ -262,7 +263,7 @@ void HTTP_AddBuildFooter(char *outbuf, int outBufSize) {
strcat_safe(outbuf,"<br>",outBufSize);
strcat_safe(outbuf,g_build_str,outBufSize);
}
int HTTP_ProcessPacket(const char *recvbuf, char *outbuf, int outBufSize) {
int HTTP_ProcessPacket(const char *recvbuf, char *outbuf, int outBufSize, http_send_fn sendpart, int socket) {
int i, j;
char tmpA[128];
char tmpB[64];
@ -688,6 +689,10 @@ int HTTP_ProcessPacket(const char *recvbuf, char *outbuf, int outBufSize) {
}
}
}
if (sendpart){
sendpart(socket, outbuf, strlen(outbuf));
outbuf[0] = 0;
}
if(iChangedRequested>0) {
PIN_SaveToFlash();
sprintf(tmpA, "Pins update - %i reqs, %i changed!<br><br>",iChangedRequested,iChanged);
@ -703,6 +708,10 @@ int HTTP_ProcessPacket(const char *recvbuf, char *outbuf, int outBufSize) {
strcat(outbuf,tmpA);
sprintf(tmpA, "<select name=\"%i\">",i);
strcat(outbuf,tmpA);
if (sendpart){
sendpart(socket, outbuf, strlen(outbuf));
outbuf[0] = 0;
}
for(j = 0; j < IOR_Total_Options; j++) {
if(j == si) {
sprintf(tmpA, "<option value=\"%i\" selected>%s</option>",j,htmlPinRoleNames[j]);
@ -710,6 +719,10 @@ int HTTP_ProcessPacket(const char *recvbuf, char *outbuf, int outBufSize) {
sprintf(tmpA, "<option value=\"%i\">%s</option>",j,htmlPinRoleNames[j]);
}
strcat(outbuf,tmpA);
if (sendpart){
sendpart(socket, outbuf, strlen(outbuf));
outbuf[0] = 0;
}
}
strcat(outbuf, "</select>");
if(ch == 0) {
@ -720,6 +733,10 @@ int HTTP_ProcessPacket(const char *recvbuf, char *outbuf, int outBufSize) {
sprintf(tmpA, "<input name=\"r%i\" type=\"text\" value=\"%s\"/>",i,tmpB);
strcat(outbuf,tmpA);
strcat(outbuf,"<br>");
if (sendpart){
sendpart(socket, outbuf, strlen(outbuf));
outbuf[0] = 0;
}
}
strcat(outbuf,"<input type=\"submit\" value=\"Save\"/></form>");
@ -868,5 +885,6 @@ int HTTP_ProcessPacket(const char *recvbuf, char *outbuf, int outBufSize) {
HTTP_AddBuildFooter(outbuf,outBufSize);
strcat(outbuf,htmlEnd);
}
i = strlen(outbuf);
return i;
}

View File

@ -7,7 +7,9 @@ extern const char htmlHeader[];
extern const char htmlEnd[];
extern const char htmlReturnToMenu[];
int HTTP_ProcessPacket(const char *recvbuf, char *outbuf, int outBufSize);
typedef int (*http_send_fn)(int fd, const char *payload, int len);
int HTTP_ProcessPacket(const char *recvbuf, char *outbuf, int outBufSize, http_send_fn send, int socket);
void http_setup(char *o, const char *type);