Correct SEGV when writing response after reading form data, add http_escape

master
teknomunk 1 year ago
parent 897cc85f52
commit e45ab8bf31

@ -0,0 +1,58 @@
#include "escape.h"
#include <strings.h>
const char* http_escape( const char* str, char* out, unsigned int limit, const char* allowed )
{
const char* result = out;
// Based on RFC 3986 Uniform Resource Identifier (URI): Generic Syntax
for(; *str; ++str ) {
switch( *str ) {
// Reserved characters (sometimes encoded)
case ':':
case '/':
case '?':
case '#':
case '@':
case '[':
case ']':
case '+':
case ',':
case '(':
case ')':
case '!':
case '*':
if( !index(allowed, *str) ) {
goto escape;
}
// Unreserved characters (Never encoded)
case 'a' ... 'z':
case 'A' ... 'Z':
case '0' ... '9':
case '~':
case '.':
case '-':
case '_':
*out = *str;
if( limit ) { --limit; ++out; }
break;
default:
{
escape:
*out = '%';
if( limit ) { --limit; ++out; }
*out = "0123456789ABCDEF"[ (*str >> 4) % 16 ];
if( limit ) { --limit; ++out; }
*out = "0123456789ABCDEF"[ (*str >> 0) % 16 ];
if( limit ) { --limit; ++out; }
}
}
}
*out = '\0';
return result;
}

@ -0,0 +1,4 @@
#pragma once
const char* http_escape( const char* str, char* out, unsigned int limit, const char* allowed );

@ -47,6 +47,7 @@ struct http_request
struct header* request_headers;
// body
FILE* response_body;
FILE* request;
bool chunked_body;
long response_size;
struct buffered_write* response_body_buffer;
@ -374,7 +375,8 @@ struct http_request* http_request_new( ucontext_t* main, int sock, const char* r
req->response_size = -1;
req->unread_post_content = -1;
req->response_body = fopencookie( (void*)req, "w+", http_response_functions );
req->response_body = fopencookie( (void*)req, "w", http_response_functions );
req->request = fopencookie( (void*)req, "r", http_response_functions );
if( -1 == fcntl( sock, F_SETFL, O_NONBLOCK ) ) {
printf( "Unable to set O_NONBLOCK on request socket, continuing with reduced capacity: %s\n", strerror(errno) );
@ -449,6 +451,8 @@ const char* string_for_status( int status_code )
switch( status_code ) {
case 200:
return "OK";
case 302:
return "Found";
default:
return "Unknown";
}
@ -497,7 +501,7 @@ FILE* http_request_get_response_body( struct http_request* req )
}
FILE* http_request_get_request_data( struct http_request* req )
{
return req->response_body;
return req->request;
}
bool http_request_send_file( struct http_request* req, const char* fs_path, const char* content_type )
{

Loading…
Cancel
Save