[asterisk-commits] rmudgett: branch rmudgett/http_persistent r417743 - /team/rmudgett/http_persi...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Jul 1 21:21:46 CDT 2014


Author: rmudgett
Date: Tue Jul  1 21:21:40 2014
New Revision: 417743

URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=417743
Log:
Extract http_request_headers_get() from httpd_process_request().

Done so a 4k buffer doesn't stay on the stack when it is no longer needed.

Modified:
    team/rmudgett/http_persistent/main/http.c

Modified: team/rmudgett/http_persistent/main/http.c
URL: http://svnview.digium.com/svn/asterisk/team/rmudgett/http_persistent/main/http.c?view=diff&rev=417743&r1=417742&r2=417743
==============================================================================
--- team/rmudgett/http_persistent/main/http.c (original)
+++ team/rmudgett/http_persistent/main/http.c Tue Jul  1 21:21:40 2014
@@ -1637,21 +1637,103 @@
 /*! Limit the number of request headers in case the sender is being ridiculous. */
 #define MAX_HTTP_REQUEST_HEADERS	100
 
+/*!
+ * \internal
+ * \brief Read the request headers.
+ * \since 12.4.0
+ *
+ * \param ser HTTP TCP/TLS session object.
+ * \param headers Where to put the request headers list pointer.
+ *
+ * \retval 0 on success.
+ * \retval -1 on error.
+ */
+static int http_request_headers_get(struct ast_tcptls_session_instance *ser, struct ast_variable **headers)
+{
+	struct ast_variable *tail = *headers;
+	int remaining_headers;
+	char header_line[MAX_HTTP_LINE_LENGTH];
+
+	remaining_headers = MAX_HTTP_REQUEST_HEADERS;
+	for (;;) {
+		char *name;
+		char *value;
+
+		if (!fgets(header_line, sizeof(header_line), ser->f)) {
+			ast_http_error(ser, 400, "Bad Request", "Timeout");
+			return -1;
+		}
+
+		/* Trim trailing characters */
+		ast_trim_blanks(header_line);
+		if (ast_strlen_zero(header_line)) {
+			/* A blank line ends the request header section. */
+			break;
+		}
+
+		value = header_line;
+		name = strsep(&value, ":");
+		if (!value) {
+			continue;
+		}
+
+		value = ast_skip_blanks(value);
+		if (ast_strlen_zero(value) || ast_strlen_zero(name)) {
+			continue;
+		}
+
+		ast_trim_blanks(name);
+
+		if (!remaining_headers--) {
+			/* Too many headers. */
+			ast_http_error(ser, 413, "Request Entity Too Large", "Too many headers");
+			return -1;
+		}
+		if (!*headers) {
+			*headers = ast_variable_new(name, value, __FILE__);
+			tail = *headers;
+		} else {
+			tail->next = ast_variable_new(name, value, __FILE__);
+			tail = tail->next;
+		}
+		if (!tail) {
+			/*
+			 * Variable allocation failure.
+			 * Try to make some room.
+			 */
+			ast_variables_destroy(*headers);
+			*headers = NULL;
+
+			ast_http_error(ser, 500, "Server Error", "Out of memory");
+			return -1;
+		}
+	}
+
+	return 0;
+}
+
+/*!
+ * \internal
+ * \brief Process a HTTP request.
+ * \since 12.4.0
+ *
+ * \param ser HTTP TCP/TLS session object.
+ *
+ * \retval 0 Continue and process the next HTTP request.
+ * \retval -1 Fatal HTTP connection error.  Force the HTTP connection closed.
+ */
 static int httpd_process_request(struct ast_tcptls_session_instance *ser)
 {
 	RAII_VAR(struct ast_variable *, headers, NULL, ast_variables_destroy);
-	struct ast_variable *tail = headers;
 	char *uri;
 	char *method;
 	const char *transfer_encoding;
 	struct http_worker_private_data *request;
 	enum ast_http_method http_method = AST_HTTP_UNKNOWN;
-	int remaining_headers;
 	int res;
-	char buf[MAX_HTTP_LINE_LENGTH];
-	char header_line[MAX_HTTP_LINE_LENGTH];
-
-	if (!fgets(buf, sizeof(buf), ser->f)) {
+	char request_line[MAX_HTTP_LINE_LENGTH];
+
+	if (!fgets(request_line, sizeof(request_line), ser->f)) {
 		return -1;
 	}
 
@@ -1660,7 +1742,7 @@
 	http_request_tracking_init(request);
 
 	/* Get method */
-	method = ast_skip_blanks(buf);
+	method = ast_skip_blanks(request_line);
 	uri = ast_skip_nonblanks(method);
 	if (*uri) {
 		*uri++ = '\0';
@@ -1693,59 +1775,8 @@
 	}
 
 	/* process "Request Headers" lines */
-	remaining_headers = MAX_HTTP_REQUEST_HEADERS;
-	for (;;) {
-		char *name;
-		char *value;
-
-		if (!fgets(header_line, sizeof(header_line), ser->f)) {
-			ast_http_error(ser, 400, "Bad Request", "Timeout");
-			return -1;
-		}
-
-		/* Trim trailing characters */
-		ast_trim_blanks(header_line);
-		if (ast_strlen_zero(header_line)) {
-			/* A blank line ends the request header section. */
-			break;
-		}
-
-		value = header_line;
-		name = strsep(&value, ":");
-		if (!value) {
-			continue;
-		}
-
-		value = ast_skip_blanks(value);
-		if (ast_strlen_zero(value) || ast_strlen_zero(name)) {
-			continue;
-		}
-
-		ast_trim_blanks(name);
-
-		if (!remaining_headers--) {
-			/* Too many headers. */
-			ast_http_error(ser, 413, "Request Entity Too Large", "Too many headers");
-			return -1;
-		}
-		if (!headers) {
-			headers = ast_variable_new(name, value, __FILE__);
-			tail = headers;
-		} else {
-			tail->next = ast_variable_new(name, value, __FILE__);
-			tail = tail->next;
-		}
-		if (!tail) {
-			/*
-			 * Variable allocation failure.
-			 * Try to make some room.
-			 */
-			ast_variables_destroy(headers);
-			headers = NULL;
-
-			ast_http_error(ser, 500, "Server Error", "Out of memory");
-			return -1;
-		}
+	if (http_request_headers_get(ser, &headers)) {
+		return -1;
 	}
 
 	transfer_encoding = get_transfer_encoding(headers);




More information about the asterisk-commits mailing list