forked from cory/tildefriends
		
	Minor cleanup. Make http.c trace its callbacks.
git-svn-id: https://www.unprompted.com/svn/projects/tildefriends/trunk@4728 ed5197a5-7fde-0310-b194-c3ffbd925b24
This commit is contained in:
		
							
								
								
									
										18
									
								
								src/http.c
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								src/http.c
									
									
									
									
									
								
							| @@ -3,6 +3,7 @@ | |||||||
| #include "log.h" | #include "log.h" | ||||||
| #include "mem.h" | #include "mem.h" | ||||||
| #include "tls.h" | #include "tls.h" | ||||||
|  | #include "trace.h" | ||||||
| #include "util.js.h" | #include "util.js.h" | ||||||
|  |  | ||||||
| #include "picohttpparser.h" | #include "picohttpparser.h" | ||||||
| @@ -48,6 +49,7 @@ typedef struct _tf_http_connection_t | |||||||
| 	int headers_length; | 	int headers_length; | ||||||
|  |  | ||||||
| 	tf_http_callback_t* callback; | 	tf_http_callback_t* callback; | ||||||
|  | 	const char* trace_name; | ||||||
| 	tf_http_request_t* request; | 	tf_http_request_t* request; | ||||||
| 	void* user_data; | 	void* user_data; | ||||||
|  |  | ||||||
| @@ -89,6 +91,7 @@ typedef struct _tf_http_t | |||||||
|  |  | ||||||
| 	int pending_closes; | 	int pending_closes; | ||||||
| 	uv_loop_t* loop; | 	uv_loop_t* loop; | ||||||
|  | 	tf_trace_t* trace; | ||||||
|  |  | ||||||
| 	void* user_data; | 	void* user_data; | ||||||
| 	tf_http_cleanup_t* user_data_cleanup; | 	tf_http_cleanup_t* user_data_cleanup; | ||||||
| @@ -110,13 +113,18 @@ tf_http_t* tf_http_create(uv_loop_t* loop) | |||||||
| 	return http; | 	return http; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | void tf_http_set_trace(tf_http_t* http, tf_trace_t* trace) | ||||||
|  | { | ||||||
|  | 	http->trace = trace; | ||||||
|  | } | ||||||
|  |  | ||||||
| void _http_allocate_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) | void _http_allocate_buffer(uv_handle_t* handle, size_t suggested_size, uv_buf_t* buf) | ||||||
| { | { | ||||||
| 	tf_http_connection_t* connection = handle->data; | 	tf_http_connection_t* connection = handle->data; | ||||||
| 	*buf = uv_buf_init(connection->incoming, sizeof(connection->incoming)); | 	*buf = uv_buf_init(connection->incoming, sizeof(connection->incoming)); | ||||||
| } | } | ||||||
|  |  | ||||||
| bool _http_find_handler(tf_http_t* http, const char* path, tf_http_callback_t** out_callback, void** out_user_data) | bool _http_find_handler(tf_http_t* http, const char* path, tf_http_callback_t** out_callback, const char** out_trace_name, void** out_user_data) | ||||||
| { | { | ||||||
| 	for (int i = 0; i < http->handlers_count; i++) | 	for (int i = 0; i < http->handlers_count; i++) | ||||||
| 	{ | 	{ | ||||||
| @@ -125,6 +133,7 @@ bool _http_find_handler(tf_http_t* http, const char* path, tf_http_callback_t** | |||||||
| 			(strncmp(path, http->handlers[i].pattern, strlen(http->handlers[i].pattern)) == 0 && path[strlen(http->handlers[i].pattern)] == '/')) | 			(strncmp(path, http->handlers[i].pattern, strlen(http->handlers[i].pattern)) == 0 && path[strlen(http->handlers[i].pattern)] == '/')) | ||||||
| 		{ | 		{ | ||||||
| 			*out_callback = http->handlers[i].callback; | 			*out_callback = http->handlers[i].callback; | ||||||
|  | 			*out_trace_name = http->handlers[i].pattern; | ||||||
| 			*out_user_data = http->handlers[i].user_data; | 			*out_user_data = http->handlers[i].user_data; | ||||||
| 			return true; | 			return true; | ||||||
| 		} | 		} | ||||||
| @@ -302,11 +311,13 @@ static void _http_add_body_bytes(tf_http_connection_t* connection, const void* d | |||||||
| 				{ | 				{ | ||||||
| 					if (connection->request->on_message) | 					if (connection->request->on_message) | ||||||
| 					{ | 					{ | ||||||
|  | 						tf_trace_begin(connection->http->trace, connection->trace_name ? connection->trace_name : "websocket"); | ||||||
| 						connection->request->on_message( | 						connection->request->on_message( | ||||||
| 							connection->request, | 							connection->request, | ||||||
| 							connection->fragment_length ? connection->fragment_op_code : op_code, | 							connection->fragment_length ? connection->fragment_op_code : op_code, | ||||||
| 							connection->fragment_length ? connection->fragment : message, | 							connection->fragment_length ? connection->fragment : message, | ||||||
| 							connection->fragment_length ? connection->fragment_length : length); | 							connection->fragment_length ? connection->fragment_length : length); | ||||||
|  | 						tf_trace_end(connection->http->trace); | ||||||
| 					} | 					} | ||||||
| 					connection->fragment_length = 0; | 					connection->fragment_length = 0; | ||||||
| 				} | 				} | ||||||
| @@ -355,7 +366,9 @@ static void _http_add_body_bytes(tf_http_connection_t* connection, const void* d | |||||||
| 			connection->request = request; | 			connection->request = request; | ||||||
|  |  | ||||||
| 			tf_http_request_ref(request); | 			tf_http_request_ref(request); | ||||||
|  | 			tf_trace_begin(connection->http->trace, connection->trace_name ? connection->trace_name : "http"); | ||||||
| 			connection->callback(request); | 			connection->callback(request); | ||||||
|  | 			tf_trace_end(connection->http->trace); | ||||||
| 			tf_http_request_release(request); | 			tf_http_request_release(request); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| @@ -424,9 +437,10 @@ static size_t _http_on_read_plain_internal(tf_http_connection_t* connection, con | |||||||
| 				connection->body = tf_malloc(connection->content_length); | 				connection->body = tf_malloc(connection->content_length); | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			if (!_http_find_handler(connection->http, connection->path, &connection->callback, &connection->user_data) || !connection->callback) | 			if (!_http_find_handler(connection->http, connection->path, &connection->callback, &connection->trace_name, &connection->user_data) || !connection->callback) | ||||||
| 			{ | 			{ | ||||||
| 				connection->callback = _http_builtin_404_handler; | 				connection->callback = _http_builtin_404_handler; | ||||||
|  | 				connection->trace_name = "404"; | ||||||
| 			} | 			} | ||||||
| 			size_t consumed = read_size - (connection->headers_buffer_length - parse_result) - (read_size - used_read_size); | 			size_t consumed = read_size - (connection->headers_buffer_length - parse_result) - (read_size - used_read_size); | ||||||
| 			_http_add_body_bytes(connection, NULL, 0); | 			_http_add_body_bytes(connection, NULL, 0); | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ typedef struct _tf_http_connection_t tf_http_connection_t; | |||||||
| typedef struct _tf_http_request_t tf_http_request_t; | typedef struct _tf_http_request_t tf_http_request_t; | ||||||
| typedef struct _tf_http_t tf_http_t; | typedef struct _tf_http_t tf_http_t; | ||||||
| typedef struct _tf_tls_context_t tf_tls_context_t; | typedef struct _tf_tls_context_t tf_tls_context_t; | ||||||
|  | typedef struct _tf_trace_t tf_trace_t; | ||||||
| typedef struct uv_loop_s uv_loop_t; | typedef struct uv_loop_s uv_loop_t; | ||||||
|  |  | ||||||
| typedef void (tf_http_message_callback)(tf_http_request_t* request, int op_code, const void* data, size_t size); | typedef void (tf_http_message_callback)(tf_http_request_t* request, int op_code, const void* data, size_t size); | ||||||
| @@ -33,6 +34,7 @@ typedef void (tf_http_callback_t)(tf_http_request_t* request); | |||||||
| typedef void (tf_http_cleanup_t)(void* user_data); | typedef void (tf_http_cleanup_t)(void* user_data); | ||||||
|  |  | ||||||
| tf_http_t* tf_http_create(uv_loop_t* loop); | tf_http_t* tf_http_create(uv_loop_t* loop); | ||||||
|  | void tf_http_set_trace(tf_http_t* http, tf_trace_t* trace); | ||||||
| int tf_http_listen(tf_http_t* http, int port, tf_tls_context_t* tls); | int tf_http_listen(tf_http_t* http, int port, tf_tls_context_t* tls); | ||||||
| void tf_http_add_handler(tf_http_t* http, const char* pattern, tf_http_callback_t* callback, void* user_data); | void tf_http_add_handler(tf_http_t* http, const char* pattern, tf_http_callback_t* callback, void* user_data); | ||||||
| void tf_http_respond(tf_http_request_t* request, int status, const char** headers, int headers_count, const void* body, size_t content_length); | void tf_http_respond(tf_http_request_t* request, int status, const char** headers, int headers_count, const void* body, size_t content_length); | ||||||
|   | |||||||
| @@ -302,7 +302,7 @@ static void _httpd_websocket_callback(tf_http_request_t* request) | |||||||
| 	_httpd_callback_internal(request, true); | 	_httpd_callback_internal(request, true); | ||||||
| } | } | ||||||
|  |  | ||||||
| static JSValue _httpd_all(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) | static JSValue _httpd_endpoint_all(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) | ||||||
| { | { | ||||||
| 	tf_http_t* http = JS_GetOpaque(this_val, _httpd_class_id); | 	tf_http_t* http = JS_GetOpaque(this_val, _httpd_class_id); | ||||||
| 	const char* pattern = JS_ToCString(context, argv[0]); | 	const char* pattern = JS_ToCString(context, argv[0]); | ||||||
| @@ -324,7 +324,7 @@ static JSValue _httpd_register_socket_handler(JSContext* context, JSValueConst t | |||||||
| 	return JS_UNDEFINED; | 	return JS_UNDEFINED; | ||||||
| } | } | ||||||
|  |  | ||||||
| static JSValue _httpd_start(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) | static JSValue _httpd_endpoint_start(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) | ||||||
| { | { | ||||||
| 	tf_http_t* http = JS_GetOpaque(this_val, _httpd_class_id); | 	tf_http_t* http = JS_GetOpaque(this_val, _httpd_class_id); | ||||||
| 	int port = 0; | 	int port = 0; | ||||||
| @@ -356,13 +356,13 @@ static JSValue _httpd_set_http_redirect(JSContext* context, JSValueConst this_va | |||||||
| 	return JS_UNDEFINED; | 	return JS_UNDEFINED; | ||||||
| } | } | ||||||
|  |  | ||||||
| void _httpd_finalizer(JSRuntime* runtime, JSValue value) | static void _httpd_finalizer(JSRuntime* runtime, JSValue value) | ||||||
| { | { | ||||||
| 	tf_http_t* http = JS_GetOpaque(value, _httpd_class_id); | 	tf_http_t* http = JS_GetOpaque(value, _httpd_class_id); | ||||||
| 	tf_http_destroy(http); | 	tf_http_destroy(http); | ||||||
| } | } | ||||||
|  |  | ||||||
| void _httpd_request_finalizer(JSRuntime* runtime, JSValue value) | static void _httpd_request_finalizer(JSRuntime* runtime, JSValue value) | ||||||
| { | { | ||||||
| 	tf_http_request_t* request = JS_GetOpaque(value, _httpd_request_class_id); | 	tf_http_request_t* request = JS_GetOpaque(value, _httpd_request_class_id); | ||||||
| 	tf_http_request_release(request); | 	tf_http_request_release(request); | ||||||
| @@ -377,7 +377,6 @@ static void _httpd_endpoint_trace(tf_http_request_t* request) | |||||||
|  |  | ||||||
| 	tf_task_t* task = request->user_data; | 	tf_task_t* task = request->user_data; | ||||||
| 	tf_trace_t* trace = tf_task_get_trace(task); | 	tf_trace_t* trace = tf_task_get_trace(task); | ||||||
| 	tf_trace_begin(trace, __func__); |  | ||||||
| 	char* json = tf_trace_export(trace); | 	char* json = tf_trace_export(trace); | ||||||
| 	const char* headers[] = | 	const char* headers[] = | ||||||
| 	{ | 	{ | ||||||
| @@ -386,7 +385,6 @@ static void _httpd_endpoint_trace(tf_http_request_t* request) | |||||||
| 	}; | 	}; | ||||||
| 	tf_http_respond(request, 200, headers, tf_countof(headers) / 2, json, json ? strlen(json) : 0); | 	tf_http_respond(request, 200, headers, tf_countof(headers) / 2, json, json ? strlen(json) : 0); | ||||||
| 	tf_free(json); | 	tf_free(json); | ||||||
| 	tf_trace_end(trace); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| static void _httpd_endpoint_mem(tf_http_request_t* request) | static void _httpd_endpoint_mem(tf_http_request_t* request) | ||||||
| @@ -396,10 +394,6 @@ static void _httpd_endpoint_mem(tf_http_request_t* request) | |||||||
| 		return; | 		return; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	tf_task_t* task = request->user_data; |  | ||||||
| 	tf_trace_t* trace = tf_task_get_trace(task); |  | ||||||
| 	tf_trace_begin(trace, __func__); |  | ||||||
|  |  | ||||||
| 	char* response = NULL; | 	char* response = NULL; | ||||||
| 	size_t length = 0; | 	size_t length = 0; | ||||||
|  |  | ||||||
| @@ -423,8 +417,6 @@ static void _httpd_endpoint_mem(tf_http_request_t* request) | |||||||
| 	}; | 	}; | ||||||
| 	tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, length); | 	tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, length); | ||||||
| 	tf_free(response); | 	tf_free(response); | ||||||
|  |  | ||||||
| 	tf_trace_end(trace); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| static void _httpd_endpoint_disconnections(tf_http_request_t* request) | static void _httpd_endpoint_disconnections(tf_http_request_t* request) | ||||||
| @@ -435,8 +427,6 @@ static void _httpd_endpoint_disconnections(tf_http_request_t* request) | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	tf_task_t* task = request->user_data; | 	tf_task_t* task = request->user_data; | ||||||
| 	tf_trace_t* trace = tf_task_get_trace(task); |  | ||||||
| 	tf_trace_begin(trace, __func__); |  | ||||||
| 	char* response = tf_task_get_disconnections(task); | 	char* response = tf_task_get_disconnections(task); | ||||||
| 	const char* headers[] = | 	const char* headers[] = | ||||||
| 	{ | 	{ | ||||||
| @@ -445,7 +435,6 @@ static void _httpd_endpoint_disconnections(tf_http_request_t* request) | |||||||
| 	}; | 	}; | ||||||
| 	tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, response ? strlen(response) : 0); | 	tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, response ? strlen(response) : 0); | ||||||
| 	tf_free(response); | 	tf_free(response); | ||||||
| 	tf_trace_end(trace); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| static void _httpd_endpoint_hitches(tf_http_request_t* request) | static void _httpd_endpoint_hitches(tf_http_request_t* request) | ||||||
| @@ -456,8 +445,6 @@ static void _httpd_endpoint_hitches(tf_http_request_t* request) | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	tf_task_t* task = request->user_data; | 	tf_task_t* task = request->user_data; | ||||||
| 	tf_trace_t* trace = tf_task_get_trace(task); |  | ||||||
| 	tf_trace_begin(trace, __func__); |  | ||||||
| 	char* response = tf_task_get_hitches(task); | 	char* response = tf_task_get_hitches(task); | ||||||
| 	const char* headers[] = | 	const char* headers[] = | ||||||
| 	{ | 	{ | ||||||
| @@ -466,7 +453,6 @@ static void _httpd_endpoint_hitches(tf_http_request_t* request) | |||||||
| 	}; | 	}; | ||||||
| 	tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, response ? strlen(response) : 0); | 	tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, response ? strlen(response) : 0); | ||||||
| 	tf_free(response); | 	tf_free(response); | ||||||
| 	tf_trace_end(trace); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| static void _httpd_endpoint_debug(tf_http_request_t* request) | static void _httpd_endpoint_debug(tf_http_request_t* request) | ||||||
| @@ -477,8 +463,6 @@ static void _httpd_endpoint_debug(tf_http_request_t* request) | |||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	tf_task_t* task = request->user_data; | 	tf_task_t* task = request->user_data; | ||||||
| 	tf_trace_t* trace = tf_task_get_trace(task); |  | ||||||
| 	tf_trace_begin(trace, __func__); |  | ||||||
| 	char* response = tf_task_get_debug(task); | 	char* response = tf_task_get_debug(task); | ||||||
| 	const char* headers[] = | 	const char* headers[] = | ||||||
| 	{ | 	{ | ||||||
| @@ -487,7 +471,6 @@ static void _httpd_endpoint_debug(tf_http_request_t* request) | |||||||
| 	}; | 	}; | ||||||
| 	tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, response ? strlen(response) : 0); | 	tf_http_respond(request, 200, headers, tf_countof(headers) / 2, response, response ? strlen(response) : 0); | ||||||
| 	tf_free(response); | 	tf_free(response); | ||||||
| 	tf_trace_end(trace); |  | ||||||
| } | } | ||||||
|  |  | ||||||
| void tf_httpd_register(JSContext* context) | void tf_httpd_register(JSContext* context) | ||||||
| @@ -518,6 +501,7 @@ void tf_httpd_register(JSContext* context) | |||||||
| 	tf_task_t* task = tf_task_get(context); | 	tf_task_t* task = tf_task_get(context); | ||||||
| 	uv_loop_t* loop = tf_task_get_loop(task); | 	uv_loop_t* loop = tf_task_get_loop(task); | ||||||
| 	tf_http_t* http = tf_http_create(loop); | 	tf_http_t* http = tf_http_create(loop); | ||||||
|  | 	tf_http_set_trace(http, tf_task_get_trace(task)); | ||||||
| 	JS_SetOpaque(httpd, http); | 	JS_SetOpaque(httpd, http); | ||||||
|  |  | ||||||
| 	tf_http_add_handler(http, "/debug", _httpd_endpoint_debug, task); | 	tf_http_add_handler(http, "/debug", _httpd_endpoint_debug, task); | ||||||
| @@ -527,9 +511,9 @@ void tf_httpd_register(JSContext* context) | |||||||
| 	tf_http_add_handler(http, "/trace", _httpd_endpoint_trace, task); | 	tf_http_add_handler(http, "/trace", _httpd_endpoint_trace, task); | ||||||
|  |  | ||||||
| 	JS_SetPropertyStr(context, httpd, "handlers", JS_NewObject(context)); | 	JS_SetPropertyStr(context, httpd, "handlers", JS_NewObject(context)); | ||||||
| 	JS_SetPropertyStr(context, httpd, "all", JS_NewCFunction(context, _httpd_all, "all", 2)); | 	JS_SetPropertyStr(context, httpd, "all", JS_NewCFunction(context, _httpd_endpoint_all, "all", 2)); | ||||||
| 	JS_SetPropertyStr(context, httpd, "registerSocketHandler", JS_NewCFunction(context, _httpd_register_socket_handler, "register_socket_handler", 2)); | 	JS_SetPropertyStr(context, httpd, "registerSocketHandler", JS_NewCFunction(context, _httpd_register_socket_handler, "register_socket_handler", 2)); | ||||||
| 	JS_SetPropertyStr(context, httpd, "start", JS_NewCFunction(context, _httpd_start, "start", 2)); | 	JS_SetPropertyStr(context, httpd, "start", JS_NewCFunction(context, _httpd_endpoint_start, "start", 2)); | ||||||
| 	JS_SetPropertyStr(context, httpd, "set_http_redirect", JS_NewCFunction(context, _httpd_set_http_redirect, "set_http_redirect", 1)); | 	JS_SetPropertyStr(context, httpd, "set_http_redirect", JS_NewCFunction(context, _httpd_set_http_redirect, "set_http_redirect", 1)); | ||||||
| 	JS_SetPropertyStr(context, global, "httpd", httpd); | 	JS_SetPropertyStr(context, global, "httpd", httpd); | ||||||
| 	JS_FreeValue(context, global); | 	JS_FreeValue(context, global); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user