@@ -430,6 +430,61 @@ static int proxy_parse(const char *proxy, struct flb_http_client *c)
430430 return 0 ;
431431}
432432
433+ static int add_host_and_content_length (struct flb_http_client * c )
434+ {
435+ int len ;
436+ flb_sds_t tmp ;
437+ flb_sds_t host ;
438+ char * out_host ;
439+ int out_port ;
440+ struct flb_upstream * u = c -> u_conn -> u ;
441+
442+ if (!c -> host ) {
443+ out_host = u -> tcp_host ;
444+ }
445+ else {
446+ out_host = (char * ) c -> host ;
447+ }
448+
449+ len = strlen (out_host );
450+ host = flb_sds_create_size (len + 32 );
451+ if (!host ) {
452+ flb_error ("[http_client] cannot create temporal buffer" );
453+ return -1 ;
454+ }
455+
456+ if (c -> port == 0 ) {
457+ out_port = u -> tcp_port ;
458+ }
459+ else {
460+ out_port = c -> port ;
461+ }
462+
463+ tmp = flb_sds_printf (& host , "%s:%i" , out_host , out_port );
464+ if (!tmp ) {
465+ flb_sds_destroy (host );
466+ flb_error ("[http_client] cannot compose temporary host header" );
467+ return -1 ;
468+ }
469+
470+ flb_http_add_header (c , "Host" , 4 , host , flb_sds_len (host ));
471+ flb_sds_destroy (host );
472+
473+ /* Content-Length */
474+ if (c -> body_len >= 0 ) {
475+ tmp = flb_malloc (32 );
476+ if (!tmp ) {
477+ flb_errno ();
478+ return -1 ;
479+ }
480+ len = snprintf (tmp , sizeof (tmp ) - 1 , "%i" , c -> body_len );
481+ flb_http_add_header (c , "Content-Length" , 14 , tmp , len );
482+ flb_free (tmp );
483+ }
484+
485+ return 0 ;
486+ }
487+
433488struct flb_http_client * flb_http_client (struct flb_upstream_conn * u_conn ,
434489 int method , const char * uri ,
435490 const char * body , size_t body_len ,
@@ -512,6 +567,8 @@ struct flb_http_client *flb_http_client(struct flb_upstream_conn *u_conn,
512567 c -> flags = flags ;
513568 mk_list_init (& c -> headers );
514569
570+ add_host_and_content_length (c );
571+
515572 /* Check if we have a query string */
516573 p = strchr (uri , '?' );
517574 if (p ) {
@@ -745,90 +802,11 @@ static int http_header_push(struct flb_http_client *c, struct flb_kv *header)
745802 return 0 ;
746803}
747804
748- static int header_exists (struct flb_http_client * c , char * header_buf , int len )
749- {
750- int found = FLB_FALSE ;
751- struct mk_list * head ;
752- struct flb_kv * header ;
753-
754- mk_list_foreach (head , & c -> headers ) {
755- header = mk_list_entry (head , struct flb_kv , _head );
756- if (flb_sds_len (header -> key ) != len ) {
757- continue ;
758- }
759-
760- if (strncasecmp (header -> key , header_buf , len ) == 0 ) {
761- found = FLB_TRUE ;
762- break ;
763- }
764- }
765-
766- return found ;
767- }
768-
769805static int http_headers_compose (struct flb_http_client * c )
770806{
771807 int ret ;
772- int len ;
773- flb_sds_t tmp ;
774- flb_sds_t host ;
775- char * out_host ;
776- int out_port ;
777808 struct mk_list * head ;
778809 struct flb_kv * header ;
779- struct flb_upstream * u = c -> u_conn -> u ;
780-
781- /* Check if the 'Host' header exists, if is missing, just add it */
782- ret = header_exists (c , "Host" , 4 );
783- if (ret == FLB_FALSE ) {
784- if (!c -> host ) {
785- out_host = u -> tcp_host ;
786- }
787- else {
788- out_host = (char * ) c -> host ;
789- }
790-
791- len = strlen (out_host );
792- host = flb_sds_create_size (len + 32 );
793- if (!host ) {
794- flb_error ("[http_client] cannot create temporal buffer" );
795- return -1 ;
796- }
797-
798- if (c -> port == 0 ) {
799- out_port = u -> tcp_port ;
800- }
801- else {
802- out_port = c -> port ;
803- }
804-
805- tmp = flb_sds_printf (& host , "%s:%i" , out_host , out_port );
806- if (!tmp ) {
807- flb_sds_destroy (host );
808- flb_error ("[http_client] cannot compose temporal host header" );
809- return -1 ;
810- }
811-
812- flb_http_add_header (c ,
813- "Host" , 4 ,
814- host , flb_sds_len (host ));
815- flb_sds_destroy (host );
816- }
817-
818- /* Content-Length */
819- ret = header_exists (c , "Content-Length" , 14 );
820- if (ret == FLB_FALSE ) {
821- if (c -> body_len >= 0 ) {
822- tmp = flb_malloc (32 );
823- if (!tmp ) {
824- flb_errno ();
825- return -1 ;
826- }
827- len = snprintf (tmp , sizeof (tmp ) - 1 , "%i" , c -> body_len );
828- flb_http_add_header (c , "Content-Length" , 14 , tmp , len );
829- flb_free (tmp );
830- }
831- }
832810
833811 /* Push header list to one buffer */
834812 mk_list_foreach (head , & c -> headers ) {
0 commit comments