[asterisk-commits] dlee: branch dlee/ASTERISK-22685-json-body r402851 - /team/dlee/ASTERISK-2268...
SVN commits to the Asterisk project
asterisk-commits at lists.digium.com
Fri Nov 15 09:27:38 CST 2013
Author: dlee
Date: Fri Nov 15 09:27:35 2013
New Revision: 402851
URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=402851
Log:
Review feedback.
* s/ast_free_ptr/ast_free/
* Cleaned up ast_http_get_json
Modified:
team/dlee/ASTERISK-22685-json-body/main/http.c
Modified: team/dlee/ASTERISK-22685-json-body/main/http.c
URL: http://svnview.digium.com/svn/asterisk/team/dlee/ASTERISK-22685-json-body/main/http.c?view=diff&rev=402851&r1=402850&r2=402851
==============================================================================
--- team/dlee/ASTERISK-22685-json-body/main/http.c (original)
+++ team/dlee/ASTERISK-22685-json-body/main/http.c Fri Nov 15 09:27:35 2013
@@ -608,35 +608,54 @@
#define MAX_POST_CONTENT 1025
+static const 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) {
+ return v->value;
+ }
+ }
+
+ /* Missing content type; assume empty string */
+ return "";
+}
+
+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;
+}
+
struct ast_json *ast_http_get_json(
struct ast_tcptls_session_instance *ser, struct ast_variable *headers)
{
int content_length = 0;
int res;
- struct ast_variable *v;
struct ast_json *body;
- RAII_VAR(char *, buf, NULL, ast_free_ptr);
+ RAII_VAR(char *, buf, NULL, ast_free);
/* Use errno to distinguish errors from no body */
errno = 0;
- for (v = headers; v; v = v->next) {
- if (!strcasecmp(v->name, "Content-Type")) {
- if (strcasecmp(v->value, "application/json")) {
- return NULL;
- }
- break;
- }
- }
-
- for (v = headers; v; v = v->next) {
- if (!strcasecmp(v->name, "Content-Length")) {
- content_length = atoi(v->value);
- break;
- }
- }
+ if (strcasecmp(get_content_type(headers), "application/json") != 0) {
+ /* Content type is not JSON */
+ return NULL;
+ }
+
+ content_length = get_content_length(headers);
if (content_length <= 0) {
+ /* No content (or streaming content). */
return NULL;
}
@@ -686,21 +705,12 @@
char *buf, *var, *val;
int res;
- for (v = headers; v; v = v->next) {
- if (!strcasecmp(v->name, "Content-Type")) {
- if (strcasecmp(v->value, "application/x-www-form-urlencoded")) {
- return NULL;
- }
- break;
- }
- }
-
- for (v = headers; v; v = v->next) {
- if (!strcasecmp(v->name, "Content-Length")) {
- content_length = atoi(v->value);
- break;
- }
- }
+ if (strcasecmp(get_content_type(headers), "application/x-www-form-urlencoded") != 0) {
+ /* Content type is not form data */
+ return NULL;
+ }
+
+ content_length = get_content_length(headers);
if (content_length <= 0) {
return NULL;
More information about the asterisk-commits
mailing list