@@ -397,7 +397,7 @@ static ssize_t parse_payload_json(struct flb_http *ctx, flb_sds_t tag,
397397}
398398
399399static ssize_t parse_payload_urlencoded (struct flb_http * ctx , flb_sds_t tag ,
400- char * payload , size_t size )
400+ char * payload , size_t size )
401401{
402402 int i ;
403403 int idx = 0 ;
@@ -703,8 +703,8 @@ int http_prot_handle_error(struct flb_http *ctx, struct http_conn *conn,
703703
704704/* New gen HTTP server */
705705
706- static int send_response_ng (struct flb_http_response * response ,
707- int http_status ,
706+ static int send_response_ng (struct flb_http_response * response ,
707+ int http_status ,
708708 char * message )
709709{
710710 struct mk_list * header_iterator ;
@@ -730,12 +730,12 @@ static int send_response_ng(struct flb_http_response *response,
730730 flb_http_response_set_message (response , "Forbidden" );
731731 }
732732
733- if (http_status == 200 ||
733+ if (http_status == 200 ||
734734 http_status == 201 ||
735735 http_status == 204 ) {
736736
737- flb_config_map_foreach (header_iterator ,
738- header_pair ,
737+ flb_config_map_foreach (header_iterator ,
738+ header_pair ,
739739 context -> success_headers ) {
740740 header_name = mk_list_entry_first (header_pair -> val .list ,
741741 struct flb_slist_entry ,
@@ -745,15 +745,15 @@ static int send_response_ng(struct flb_http_response *response,
745745 struct flb_slist_entry ,
746746 _head );
747747
748- flb_http_response_set_header (response ,
749- header_name -> str , 0 ,
748+ flb_http_response_set_header (response ,
749+ header_name -> str , 0 ,
750750 header_value -> str , 0 );
751751 }
752752 }
753753
754754 if (message != NULL ) {
755- flb_http_response_set_body (response ,
756- (unsigned char * ) message ,
755+ flb_http_response_set_body (response ,
756+ (unsigned char * ) message ,
757757 strlen (message ));
758758 }
759759
@@ -829,7 +829,7 @@ static int process_pack_ng(struct flb_http *ctx, flb_sds_t tag, char *buf, size_
829829 }
830830
831831 flb_log_event_encoder_reset (& ctx -> log_encoder );
832- }
832+ }
833833 else if (result .data .type == MSGPACK_OBJECT_ARRAY ) {
834834 obj = & result .data ;
835835 for (i = 0 ; i < obj -> via .array .size ; i ++ )
@@ -896,7 +896,7 @@ static int process_pack_ng(struct flb_http *ctx, flb_sds_t tag, char *buf, size_
896896 }
897897
898898 break ;
899- }
899+ }
900900 else {
901901 flb_plg_error (ctx -> ins , "skip record from invalid type: %i" ,
902902 result .data .type );
@@ -955,109 +955,14 @@ static ssize_t parse_payload_json_ng(flb_sds_t tag,
955955 return 0 ;
956956}
957957
958- static ssize_t parse_payload_urlencoded_ng (flb_sds_t tag ,
959- struct flb_http_request * request )
960- {
961- struct mk_list * kvs ;
962- struct mk_list * head = NULL ;
963- struct flb_split_entry * cur = NULL ;
964- char * * keys = NULL ;
965- char * * vals = NULL ;
966- char * sep ;
967- char * start ;
968- int idx = 0 ;
969- int ret = -1 ;
970- msgpack_packer pck ;
971- msgpack_sbuffer sbuf ;
972- struct flb_http * ctx ;
973- char * payload ;
974-
975- ctx = (struct flb_http * ) request -> stream -> user_data ;
976- payload = (char * ) request -> body ;
977-
978- /* initialize buffers */
979- msgpack_sbuffer_init (& sbuf );
980- msgpack_packer_init (& pck , & sbuf , msgpack_sbuffer_write );
981-
982- kvs = flb_utils_split (payload , '&' , -1 );
983- if (kvs == NULL ) {
984- goto split_error ;
985- }
986-
987- keys = flb_calloc (mk_list_size (kvs ), sizeof (char * ));
988- if (keys == NULL ) {
989- goto keys_calloc_error ;
990- }
991-
992- vals = flb_calloc (mk_list_size (kvs ), sizeof (char * ));
993- if (vals == NULL ) {
994- goto vals_calloc_error ;
995- }
996-
997- mk_list_foreach (head , kvs ) {
998- cur = mk_list_entry (head , struct flb_split_entry , _head );
999- if (cur -> value [0 ] == '\n' ) {
1000- start = & cur -> value [1 ];
1001- } else {
1002- start = cur -> value ;
1003- }
1004- sep = strchr (start , '=' );
1005- if (sep == NULL ) {
1006- vals [idx ] = NULL ;
1007- continue ;
1008- }
1009- * sep ++ = '\0' ;
1010-
1011- keys [idx ] = flb_sds_create_len (start , strlen (start ));
1012- vals [idx ] = flb_sds_create_len (sep , strlen (sep ));
1013-
1014- flb_sds_trim (keys [idx ]);
1015- flb_sds_trim (vals [idx ]);
1016- idx ++ ;
1017- }
1018-
1019- msgpack_pack_map (& pck , mk_list_size (kvs ));
1020- for (idx = 0 ; idx < mk_list_size (kvs ); idx ++ ) {
1021- msgpack_pack_str (& pck , flb_sds_len (keys [idx ]));
1022- msgpack_pack_str_body (& pck , keys [idx ], flb_sds_len (keys [idx ]));
1023-
1024- if (sds_uri_decode (vals [idx ]) != 0 ) {
1025- goto decode_error ;
1026- } else {
1027- msgpack_pack_str (& pck , flb_sds_len (vals [idx ]));
1028- msgpack_pack_str_body (& pck , vals [idx ], strlen (vals [idx ]));
1029- }
1030- }
1031-
1032- ret = process_pack (ctx , tag , sbuf .data , sbuf .size );
1033-
1034- decode_error :
1035- for (idx = 0 ; idx < mk_list_size (kvs ); idx ++ ) {
1036- if (keys [idx ]) {
1037- flb_sds_destroy (keys [idx ]);
1038- }
1039- if (vals [idx ]) {
1040- flb_sds_destroy (vals [idx ]);
1041- }
1042- }
1043- flb_free (vals );
1044- vals_calloc_error :
1045- flb_free (keys );
1046- keys_calloc_error :
1047- flb_utils_split_free (kvs );
1048- split_error :
1049- msgpack_sbuffer_destroy (& sbuf );
1050- return ret ;
1051- }
1052-
1053958static int process_payload_ng (flb_sds_t tag ,
1054- struct flb_http_request * request ,
1055- struct flb_http_response * response )
959+ struct flb_http_request * request ,
960+ struct flb_http_response * response )
1056961{
1057- int type ;
962+ int type = -1 ;
963+ cfl_sds_t payload ;
964+ struct flb_http * ctx ;
1058965
1059- type = -1 ;
1060-
1061966 if (request -> content_type == NULL ) {
1062967 send_response_ng (response , 400 , "error: header 'Content-Type' is not set\n" );
1063968 return -1 ;
@@ -1076,16 +981,21 @@ static int process_payload_ng(flb_sds_t tag,
1076981 return -1 ;
1077982 }
1078983
1079- if (request -> body == NULL ||
984+ if (request -> body == NULL ||
1080985 cfl_sds_len (request -> body ) == 0 ) {
1081986 send_response_ng (response , 400 , "error: no payload found\n" );
1082987 return -1 ;
1083988 }
1084989
1085990 if (type == HTTP_CONTENT_JSON ) {
1086991 parse_payload_json_ng (tag , request );
1087- } else if (type == HTTP_CONTENT_URLENCODED ) {
1088- parse_payload_urlencoded_ng (tag , request );
992+ }
993+ else if (type == HTTP_CONTENT_URLENCODED ) {
994+ ctx = (struct flb_http * ) request -> stream -> user_data ;
995+ payload = (char * ) request -> body ;
996+ if (payload ) {
997+ parse_payload_urlencoded (ctx , tag , payload , cfl_sds_len (payload ));
998+ }
1089999 }
10901000
10911001 return 0 ;
@@ -1098,10 +1008,9 @@ int http_prot_handle_ng(struct flb_http_request *request,
10981008 int ret ;
10991009 int len ;
11001010 flb_sds_t tag ;
1101- struct flb_http * context ;
1102-
1103- context = (struct flb_http * ) response -> stream -> user_data ;
1011+ struct flb_http * ctx ;
11041012
1013+ ctx = (struct flb_http * ) response -> stream -> user_data ;
11051014 if (request -> path [0 ] != '/' ) {
11061015 send_response_ng (response , 400 , "error: invalid request\n" );
11071016 return -1 ;
@@ -1130,7 +1039,7 @@ int http_prot_handle_ng(struct flb_http_request *request,
11301039
11311040 /* ToDo: Fix me */
11321041 /* HTTP/1.1 needs Host header */
1133- if (request -> protocol_version == HTTP_PROTOCOL_HTTP1 &&
1042+ if (request -> protocol_version == HTTP_PROTOCOL_HTTP1 &&
11341043 request -> host == NULL ) {
11351044 flb_sds_destroy (tag );
11361045
@@ -1145,10 +1054,9 @@ int http_prot_handle_ng(struct flb_http_request *request,
11451054 }
11461055
11471056 ret = process_payload_ng (tag , request , response );
1148-
11491057 flb_sds_destroy (tag );
11501058
1151- send_response_ng (response , context -> successful_response_code , NULL );
1059+ send_response_ng (response , ctx -> successful_response_code , NULL );
11521060
11531061 return ret ;
11541062}
0 commit comments