diff options
Diffstat (limited to 'ot_http.c')
-rw-r--r-- | ot_http.c | 32 |
1 files changed, 30 insertions, 2 deletions
@@ -10,6 +10,7 @@ | |||
10 | #include <stdio.h> | 10 | #include <stdio.h> |
11 | #include <string.h> | 11 | #include <string.h> |
12 | #include <unistd.h> | 12 | #include <unistd.h> |
13 | #include <pthread.h> | ||
13 | 14 | ||
14 | /* Libowfat */ | 15 | /* Libowfat */ |
15 | #include "byte.h" | 16 | #include "byte.h" |
@@ -79,11 +80,12 @@ static void http_senddata( const int64 sock, struct ot_workstruct *ws ) { | |||
79 | #define HTTPERROR_400_PARAM return http_issue_error( sock, ws, CODE_HTTPERROR_400_PARAM ) | 80 | #define HTTPERROR_400_PARAM return http_issue_error( sock, ws, CODE_HTTPERROR_400_PARAM ) |
80 | #define HTTPERROR_400_COMPACT return http_issue_error( sock, ws, CODE_HTTPERROR_400_COMPACT ) | 81 | #define HTTPERROR_400_COMPACT return http_issue_error( sock, ws, CODE_HTTPERROR_400_COMPACT ) |
81 | #define HTTPERROR_400_DOUBLEHASH return http_issue_error( sock, ws, CODE_HTTPERROR_400_PARAM ) | 82 | #define HTTPERROR_400_DOUBLEHASH return http_issue_error( sock, ws, CODE_HTTPERROR_400_PARAM ) |
83 | #define HTTPERROR_402_NOTMODEST return http_issue_error( sock, ws, CODE_HTTPERROR_402_NOTMODEST ) | ||
82 | #define HTTPERROR_403_IP return http_issue_error( sock, ws, CODE_HTTPERROR_403_IP ) | 84 | #define HTTPERROR_403_IP return http_issue_error( sock, ws, CODE_HTTPERROR_403_IP ) |
83 | #define HTTPERROR_404 return http_issue_error( sock, ws, CODE_HTTPERROR_404 ) | 85 | #define HTTPERROR_404 return http_issue_error( sock, ws, CODE_HTTPERROR_404 ) |
84 | #define HTTPERROR_500 return http_issue_error( sock, ws, CODE_HTTPERROR_500 ) | 86 | #define HTTPERROR_500 return http_issue_error( sock, ws, CODE_HTTPERROR_500 ) |
85 | ssize_t http_issue_error( const int64 sock, struct ot_workstruct *ws, int code ) { | 87 | ssize_t http_issue_error( const int64 sock, struct ot_workstruct *ws, int code ) { |
86 | char *error_code[] = { "302 Found", "400 Invalid Request", "400 Invalid Request", "400 Invalid Request", | 88 | char *error_code[] = { "302 Found", "400 Invalid Request", "400 Invalid Request", "400 Invalid Request", "402 Payment Required", |
87 | "403 Access Denied", "404 Not Found", "500 Internal Server Error" }; | 89 | "403 Access Denied", "404 Not Found", "500 Internal Server Error" }; |
88 | char *title = error_code[code]; | 90 | char *title = error_code[code]; |
89 | 91 | ||
@@ -234,12 +236,39 @@ static const ot_keywords keywords_format[] = | |||
234 | return ws->reply_size; | 236 | return ws->reply_size; |
235 | } | 237 | } |
236 | 238 | ||
239 | #ifdef WANT_MODEST_FULLSCRAPES | ||
240 | static pthread_mutex_t g_modest_fullscrape_mutex = PTHREAD_MUTEX_INITIALIZER; | ||
241 | static ot_vector g_modest_fullscrape_timeouts; | ||
242 | typedef struct { ot_ip6 ip; ot_time last_fullscrape; } ot_scrape_log; | ||
243 | #endif | ||
244 | |||
237 | #ifdef WANT_FULLSCRAPE | 245 | #ifdef WANT_FULLSCRAPE |
238 | static ssize_t http_handle_fullscrape( const int64 sock, struct ot_workstruct *ws ) { | 246 | static ssize_t http_handle_fullscrape( const int64 sock, struct ot_workstruct *ws ) { |
239 | struct http_data* cookie = io_getcookie( sock ); | 247 | struct http_data* cookie = io_getcookie( sock ); |
240 | int format = 0; | 248 | int format = 0; |
241 | tai6464 t; | 249 | tai6464 t; |
242 | 250 | ||
251 | #ifdef WANT_MODEST_FULLSCRAPES | ||
252 | { | ||
253 | ot_scrape_log this_peer, *new_peer; | ||
254 | int exactmatch; | ||
255 | memcpy( this_peer.ip, cookie->ip, sizeof(ot_ip6)); | ||
256 | this_peer.last_fullscrape = g_now_seconds; | ||
257 | pthread_mutex_lock(&g_modest_fullscrape_mutex); | ||
258 | new_peer = vector_find_or_insert( &g_modest_fullscrape_timeouts, &this_peer, sizeof(ot_scrape_log), sizeof(ot_ip6), &exactmatch ); | ||
259 | if( !new_peer ) { | ||
260 | pthread_mutex_unlock(&g_modest_fullscrape_mutex); | ||
261 | HTTPERROR_500; | ||
262 | } | ||
263 | if( exactmatch && ( this_peer.last_fullscrape - new_peer->last_fullscrape ) < OT_MODEST_PEER_TIMEOUT ) { | ||
264 | pthread_mutex_unlock(&g_modest_fullscrape_mutex); | ||
265 | HTTPERROR_402_NOTMODEST; | ||
266 | } | ||
267 | memcpy( new_peer, &this_peer, sizeof(ot_scrape_log)); | ||
268 | pthread_mutex_unlock(&g_modest_fullscrape_mutex); | ||
269 | } | ||
270 | #endif | ||
271 | |||
243 | #ifdef WANT_COMPRESSION_GZIP | 272 | #ifdef WANT_COMPRESSION_GZIP |
244 | ws->request[ws->request_size-1] = 0; | 273 | ws->request[ws->request_size-1] = 0; |
245 | if( strstr( ws->request, "gzip" ) ) { | 274 | if( strstr( ws->request, "gzip" ) ) { |
@@ -476,7 +505,6 @@ ssize_t http_handle_request( const int64 sock, struct ot_workstruct *ws ) { | |||
476 | struct http_data *cookie = io_getcookie( sock ); | 505 | struct http_data *cookie = io_getcookie( sock ); |
477 | if( loglist_check_address( cookie->ip ) ) { | 506 | if( loglist_check_address( cookie->ip ) ) { |
478 | ot_log *log = malloc( sizeof( ot_log ) ); | 507 | ot_log *log = malloc( sizeof( ot_log ) ); |
479 | printf( "Hello World\n" ); | ||
480 | if( log ) { | 508 | if( log ) { |
481 | log->size = ws->request_size; | 509 | log->size = ws->request_size; |
482 | log->data = malloc( ws->request_size ); | 510 | log->data = malloc( ws->request_size ); |