mirror of
https://github.com/openshwprojects/OpenBK7231T_App.git
synced 2026-02-11 17:25:46 +00:00
Base http server on 'http_request' structure. It holds everything needed by a respondant to a request.
This commit is contained in:
@ -39,7 +39,6 @@ static void tcp_client_thread( beken_thread_arg_t arg )
|
||||
{
|
||||
OSStatus err = kNoErr;
|
||||
int fd = (int) arg;
|
||||
int lenrx = 0;
|
||||
//fd_set readfds, errfds, readfds2;
|
||||
char *buf = NULL;
|
||||
char *reply = NULL;
|
||||
@ -50,37 +49,34 @@ static void tcp_client_thread( beken_thread_arg_t arg )
|
||||
//my_fd = fd;
|
||||
|
||||
reply = (char*) os_malloc( replyBufferSize );
|
||||
buf = (char*) os_malloc( 1024 );
|
||||
buf = (char*) os_malloc( 1026 );
|
||||
ASSERT(buf);
|
||||
|
||||
while ( 1 )
|
||||
http_request_t request;
|
||||
os_memset(&request, 0, sizeof(request));
|
||||
|
||||
request.fd = fd;
|
||||
request.received = buf;
|
||||
request.receivedLen = recv( fd, request.received, 1024, 0 );
|
||||
|
||||
request.reply = reply;
|
||||
reply[0] = '\0';
|
||||
request.replymaxlen = replyBufferSize - 1;
|
||||
|
||||
if ( request.receivedLen <= 0 )
|
||||
{
|
||||
lenrx = recv( fd, buf, 1024, 0 );
|
||||
|
||||
if ( lenrx <= 0 )
|
||||
{
|
||||
os_printf( "TCP Client is disconnected, fd: %d", fd );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
//addLog( "TCP received string %s\n",buf );
|
||||
// returns length to be sent if any
|
||||
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);
|
||||
break;
|
||||
os_printf( "TCP Client is disconnected, fd: %d", fd );
|
||||
goto exit;
|
||||
}
|
||||
|
||||
//addLog( "TCP received string %s\n",buf );
|
||||
// returns length to be sent if any
|
||||
request.received[request.receivedLen] = 0;
|
||||
int lenret = HTTP_ProcessPacket(&request);
|
||||
addLog( "TCP sending reply len %i\n",lenret );
|
||||
send( fd, reply, lenret, 0 );
|
||||
|
||||
rtos_delay_milliseconds(10);
|
||||
|
||||
exit:
|
||||
if ( err != kNoErr )
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@ -7,14 +7,44 @@ extern const char htmlHeader[];
|
||||
extern const char htmlEnd[];
|
||||
extern const char htmlReturnToMenu[];
|
||||
|
||||
typedef int (*http_send_fn)(int fd, const char *payload, int len);
|
||||
#define MAX_QUERY 16
|
||||
#define MAX_HEADERS 16
|
||||
typedef struct http_request_tag {
|
||||
char *received; // partial or whole received data, up to 1024
|
||||
int receivedLen;
|
||||
|
||||
int HTTP_ProcessPacket(const char *recvbuf, char *outbuf, int outBufSize, http_send_fn send, int socket);
|
||||
void http_setup(char *o, const char *type);
|
||||
// filled by HTTP_ProcessPacket
|
||||
int method;
|
||||
char *url;
|
||||
int numqueryitems;
|
||||
char *querynames[MAX_QUERY];
|
||||
char *queryvalues[MAX_QUERY];
|
||||
int numheaders;
|
||||
char *headers[MAX_HEADERS];
|
||||
char *bodystart; /// start start of the body (maybe all of it)
|
||||
int contentLength;
|
||||
|
||||
// used to respond
|
||||
char *reply;
|
||||
int replymaxlen;
|
||||
int fd;
|
||||
} http_request_t;
|
||||
|
||||
|
||||
int HTTP_ProcessPacket(http_request_t *request);
|
||||
void http_setup(http_request_t *request, const char *type);
|
||||
int poststr(http_request_t *request, const char *str);
|
||||
|
||||
enum {
|
||||
HTTP_ANY = -1,
|
||||
HTTP_GET = 0,
|
||||
HTTP_PUT = 1,
|
||||
HTTP_POST = 2,
|
||||
HTTP_OPTIONS = 3
|
||||
} METHODS;
|
||||
|
||||
// callback function for http
|
||||
typedef int (*http_callback_fn)(const char *payload, char *outbuf, int outBufSize);
|
||||
typedef int (*http_callback_fn)(http_request_t *request);
|
||||
// url MUST start with '/'
|
||||
// urls must be unique (i.e. you can't have /about and /aboutme or /about/me)
|
||||
int HTTP_RegisterCallback( const char *url, http_callback_fn callback);
|
||||
int HTTP_RegisterCallback( const char *url, int method, http_callback_fn callback);
|
||||
Reference in New Issue
Block a user