[svn-commits] dlee: branch dlee/ASTERISK-22486-reject-transfer-encoding r404564 - /team/dle...
SVN commits to the Digium repositories
svn-commits at lists.digium.com
Tue Dec 24 09:38:41 CST 2013
Author: dlee
Date: Tue Dec 24 09:38:38 2013
New Revision: 404564
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=404564
Log:
Extract get_header function in http.c; fixed some copy/paste errors
Modified:
team/dlee/ASTERISK-22486-reject-transfer-encoding/main/http.c
Modified: team/dlee/ASTERISK-22486-reject-transfer-encoding/main/http.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22486-reject-transfer-encoding/main/http.c?view=diff&rev=404564&r1=404563&r2=404564
==============================================================================
--- team/dlee/ASTERISK-22486-reject-transfer-encoding/main/http.c (original)
+++ team/dlee/ASTERISK-22486-reject-transfer-encoding/main/http.c Tue Dec 24 09:38:38 2013
@@ -609,6 +609,27 @@
#define MAX_POST_CONTENT 1025
/*!
+ * \brief Retrieves the header with the given field name.
+ *
+ * \param headers Headers to search.
+ * \param field_name Name of the header to find.
+ * \return Associated header value.
+ * \return \c NULL if header is not present.
+ */
+static const char *get_header(struct ast_variable *headers,
+ const char *field_name)
+{
+ struct ast_variable *v;
+
+ for (v = headers; v; v = v->next) {
+ if (!strcasecmp(v->name, field_name)) {
+ return v->value;
+ }
+ }
+ return NULL;
+}
+
+/*!
* \brief Retrieves the content type specified in the "Content-Type" header.
*
* This function only returns the "type/subtype" and any trailing parameter is
@@ -620,46 +641,51 @@
*/
static char *get_content_type(struct ast_variable *headers)
{
- struct ast_variable *v;
-
- for (v = headers; v; v = v->next) {
- if (strcasecmp(v->name, "Content-Type") == 0) {
- const char *param = strchr(v->value, ';');
- size_t size = (param ? param - v->value :
- strlen(v->value)) + 1;
- return ast_strndup(v->value, size);
- }
- }
-
- return NULL;
-}
-
+ const char *content_type = get_header(headers, "Content-Type");
+ const char *param;
+ size_t size;
+
+ if (!content_type) {
+ return NULL;
+ }
+
+ param = strchr(content_type, ';');
+ size = param ? param - content_type : strlen(content_type);
+
+ return ast_strndup(content_type, size);
+}
+
+/*!
+ * \brief Returns the value of the Content-Length header.
+ *
+ * \param headers HTTP headers.
+ * \return Value of the Content-Length header.
+ * \return 0 if header is not present, or is invalid.
+ */
static int get_content_length(struct ast_variable *headers)
{
- struct ast_variable *v;
-
- for (v = headers; v; v = v->next) {
- if (!strcasecmp(v->name, "Content-Length")) {
- return atoi(v->value);
- }
- }
-
- /* Missing content length; assume zero */
- return 0;
-}
-
+ const char *content_length = get_header(headers, "Content-Length");
+
+ if (!content_length) {
+ /* Missing content length; assume zero */
+ return 0;
+ }
+
+ /* atoi() will return 0 for invalid inputs, which is good enough for
+ * the HTTP parsing. */
+ return atoi(content_length);
+}
+
+/*!
+ * \brief Returns the value of the Transfer-Encoding header.
+ *
+ * \param headers HTTP headers.
+ * \return Value of the Transfer-Encoding header.
+ * \return 0 if header is not present, or is invalid.
+ */
static const char *get_transfer_encoding(struct ast_variable *headers)
{
- struct ast_variable *v;
-
- for (v = headers; v; v = v->next) {
- if (!strcasecmp(v->name, "Transfer-Encoding")) {
- return v->value;
- }
- }
-
- /* Missing content length; assume zero */
- return 0;
+ return get_header(headers, "Transfer-Encoding");
}
struct ast_json *ast_http_get_json(
More information about the svn-commits
mailing list