Add HTTP response code support plus other tweaks to help in responding

This commit is contained in:
btsimonh
2022-02-12 11:08:20 +00:00
parent cd0db3a262
commit cff74553fa
3 changed files with 22 additions and 6 deletions

View File

@ -57,7 +57,9 @@ 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.receivedLenmax = 1024;
request.responseCode = HTTP_RESPONSE_OK;
request.receivedLen = recv( fd, request.received, request.receivedLenmax, 0 );
request.received[request.receivedLen] = 0;
request.reply = reply;

View File

@ -48,10 +48,11 @@ Connection: keep-alive
#define DEFAULT_OTA_URL "http://raspberrypi:1880/firmware"
const char httpHeader[] = "HTTP/1.1 200 OK\nContent-type: " ; // HTTP header
const char httpHeader[] = "HTTP/1.1 %d OK\nContent-type: %s" ; // HTTP header
const char httpMimeTypeHTML[] = "text/html" ; // HTML MIME type
const char httpMimeTypeText[] = "text/plain" ; // TEXT MIME type
const char httpMimeTypeJson[] = "application/json" ; // TEXT MIME type
const char httpMimeTypeBinary[] = "application/octet-stream" ; // binary/file MIME type
const char htmlHeader[] = "<!DOCTYPE html><html><body>" ;
const char htmlEnd[] = "</body></html>" ;
const char htmlReturnToMenu[] = "<a href=\"index\">Return to menu</a>";;
@ -73,7 +74,8 @@ const char *methodNames[] = {
#define os_malloc malloc
#endif
void misc_formatUpTimeString(int totalSeconds, char *o);
int Time_getUpTimeSeconds();
typedef struct http_callback_tag {
char *url;
@ -150,8 +152,7 @@ bool http_checkUrlBase(const char *base, const char *fileName) {
}
void http_setup(http_request_t *request, const char *type){
poststr(request,httpHeader);
poststr(request,type);
hprintf128(request, httpHeader, request->responseCode, type);
poststr(request,"\r\n"); // next header
poststr(request,httpCorsHeaders);
poststr(request,"\r\n"); // end headers with double CRLF
@ -390,7 +391,7 @@ void misc_formatUpTimeString(int totalSeconds, char *o) {
int hprintf128(http_request_t *request, const char *fmt, ...){
va_list argList;
BaseType_t taken;
//BaseType_t taken;
char tmp[128];
va_start(argList, fmt);
vsprintf(tmp, fmt, argList);
@ -494,6 +495,7 @@ int HTTP_ProcessPacket(http_request_t *request) {
} while(1);
request->bodystart = p;
request->bodylen = request->receivedLen - (p - request->received);
// we will make this more general
http_getArg(urlStr,"a",tmpA,sizeof(tmpA));

View File

@ -4,17 +4,26 @@ extern const char httpHeader[]; // HTTP header
extern const char httpMimeTypeHTML[]; // HTML MIME type
extern const char httpMimeTypeText[]; // TEXT MIME type
extern const char httpMimeTypeJson[];
extern const char httpMimeTypeBinary[];
extern const char htmlHeader[];
extern const char htmlEnd[];
extern const char htmlReturnToMenu[];
extern const char *htmlPinRoleNames[];
#define HTTP_RESPONSE_OK 200
#define HTTP_RESPONSE_NOT_FOUND 404
#define HTTP_RESPONSE_SERVER_ERROR 500
#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 receivedLenmax; // sizeof received
// filled by HTTP_ProcessPacket
int method;
@ -25,7 +34,9 @@ typedef struct http_request_tag {
int numheaders;
char *headers[MAX_HEADERS];
char *bodystart; /// start start of the body (maybe all of it)
int bodylen;
int contentLength;
int responseCode;
// used to respond
char *reply;
@ -38,6 +49,7 @@ typedef struct http_request_tag {
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);
int postany(http_request_t *request, const char *str, int len);
void misc_formatUpTimeString(int totalSeconds, char *o);
// poststr with format - for results LESS THAN 128