1
0
mirror of https://github.com/lammertb/libhttp.git synced 2026-01-27 08:02:47 +03:00

Code cleanup

This commit is contained in:
Lammert Bies
2016-12-20 23:02:17 +01:00
parent 200ff09bbe
commit 5ef6904d6b
6 changed files with 37 additions and 30 deletions

View File

@@ -53,9 +53,11 @@ int XX_httplib_parse_http_message( char *buf, int len, struct httplib_request_in
if (request_length > 0) {
/* Reset attributes. DO NOT TOUCH is_ssl, remote_ip, remote_addr,
* remote_port */
ri->remote_user = ri->request_method = ri->request_uri =
ri->http_version = NULL;
ri->num_headers = 0;
ri->remote_user = NULL;
ri->request_method = NULL;
ri->request_uri = NULL;
ri->http_version = NULL;
ri->num_headers = 0;
buf[request_length - 1] = '\0';
@@ -63,10 +65,10 @@ int XX_httplib_parse_http_message( char *buf, int len, struct httplib_request_in
while (*buf != '\0' && isspace(*(unsigned char *)buf)) {
buf++;
}
start_line = XX_httplib_skip(&buf, "\r\n");
ri->request_method = XX_httplib_skip(&start_line, " ");
ri->request_uri = XX_httplib_skip(&start_line, " ");
ri->http_version = start_line;
start_line = XX_httplib_skip( &buf, "\r\n" );
ri->request_method = XX_httplib_skip( &start_line, " " );
ri->request_uri = XX_httplib_skip( &start_line, " " );
ri->http_version = start_line;
/* HTTP message could be either HTTP request:
* "GET / HTTP/1.0 ..."

View File

@@ -22,13 +22,13 @@
* THE SOFTWARE.
*
* ============
* Release: 1.8
* Release: 2.0
*/
#include "httplib_main.h"
int XX_httplib_parse_range_header( const char *header, int64_t *a, int64_t *b ) {
return sscanf(header, "bytes=%" INT64_FMT "-%" INT64_FMT, a, b);
return sscanf( header, "bytes=%" INT64_FMT "-%" INT64_FMT, a, b );
} /* XX_httplib_parse_range_header */

View File

@@ -37,21 +37,26 @@ void XX_httplib_print_dir_entry( struct de *de ) {
if ( de->file.is_directory ) XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%s", "[DIRECTORY]" );
else {
/* We use (signed) cast below because MSVC 6 compiler cannot
* convert unsigned __int64 to double. Sigh. */
/*
* We use (signed) cast below because MSVC 6 compiler cannot
* convert unsigned __int64 to double. Sigh.
*/
if ( de->file.size < 1024) XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%d", (int) de->file.size );
else if ( de->file.size < 0x100000 ) XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%.1fk", ((double)de->file.size) / 1024.0 );
else if ( de->file.size < 0x40000000 ) XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%.1fM", ((double)de->file.size) / 1048576.0 );
else XX_httplib_snprintf( de->conn, NULL, size, sizeof(size), "%.1fG", ((double)de->file.size) / 1073741824.0 );
}
/* Note: XX_httplib_snprintf will not cause a buffer overflow above.
* So, string truncation checks are not required here. */
/*
* Note: XX_httplib_snprintf will not cause a buffer overflow above.
* So, string truncation checks are not required here.
*/
tm = localtime(&de->file.last_modified);
if (tm != NULL) {
strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", tm);
} else {
if ( tm != NULL ) strftime( mod, sizeof(mod), "%d-%b-%Y %H:%M", tm );
else {
httplib_strlcpy( mod, "01-Jan-1970 00:00", sizeof(mod) );
mod[sizeof(mod) - 1] = '\0';
}

View File

@@ -22,7 +22,7 @@
* THE SOFTWARE.
*
* ============
* Release: 1.8
* Release: 2.0
*/
#include "httplib_main.h"
@@ -47,9 +47,10 @@ void XX_httplib_send_options( struct httplib_connection *conn ) {
curtime = time( NULL );
conn->status_code = 200;
conn->must_close = 1;
XX_httplib_gmt_time_string( date, sizeof(date), &curtime );
httplib_printf(conn,
httplib_printf( conn,
"HTTP/1.1 200 OK\r\n"
"Date: %s\r\n"
/* TODO: "Cache-Control" (?) */
@@ -58,7 +59,7 @@ void XX_httplib_send_options( struct httplib_connection *conn ) {
"PROPFIND, MKCOL\r\n"
"DAV: 1\r\n\r\n",
date,
XX_httplib_suggest_connection_header(conn));
XX_httplib_suggest_connection_header( conn ) );
} /* XX_httplib_send_options */

View File

@@ -22,7 +22,7 @@
* THE SOFTWARE.
*
* ============
* Release: 1.8
* Release: 2.0
*/
#include "httplib_main.h"
@@ -34,10 +34,8 @@
int XX_httplib_set_tcp_nodelay( SOCKET sock, int nodelay_on ) {
if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (SOCK_OPT_TYPE)&nodelay_on, sizeof(nodelay_on)) != 0) { /* Error */
return 1;
}
/* OK */
if ( setsockopt( sock, IPPROTO_TCP, TCP_NODELAY, (SOCK_OPT_TYPE)&nodelay_on, sizeof(nodelay_on) ) != 0 ) return 1;
return 0;
} /* XX_httplib_set_tcp_nodelay */

View File

@@ -62,12 +62,15 @@ static int alloc_vprintf( char **out_buf, char *prealloc_buf, size_t prealloc_si
va_list ap_copy;
int len;
/* Windows is not standard-compliant, and vsnprintf() returns -1 if
/*
* Windows is not standard-compliant, and vsnprintf() returns -1 if
* buffer is too small. Also, older versions of msvcrt.dll do not have
* _vscprintf(). However, if size is 0, vsnprintf() behaves correctly.
* Therefore, we make two passes: on first pass, get required message
* length.
* On second pass, actually print the message. */
* On second pass, actually print the message.
*/
va_copy(ap_copy, ap);
len = vsnprintf_impl(NULL, 0, fmt, ap_copy);
va_end(ap_copy);
@@ -90,16 +93,14 @@ static int alloc_vprintf( char **out_buf, char *prealloc_buf, size_t prealloc_si
}
/* Buffer allocation successful. Store the string there. */
va_copy(ap_copy, ap);
IGNORE_UNUSED_RESULT(
vsnprintf_impl(*out_buf, (size_t)(len) + 1, fmt, ap_copy));
vsnprintf_impl(*out_buf, (size_t)(len) + 1, fmt, ap_copy);
va_end(ap_copy);
} else {
/* The pre-allocated buffer is large enough.
* Use it to store the string and return the address. */
va_copy(ap_copy, ap);
IGNORE_UNUSED_RESULT(
vsnprintf_impl(prealloc_buf, prealloc_size, fmt, ap_copy));
vsnprintf_impl(prealloc_buf, prealloc_size, fmt, ap_copy);
va_end(ap_copy);
*out_buf = prealloc_buf;
}