@@ -441,17 +441,12 @@ struct flb_http_client *flb_http_client(struct flb_upstream_conn *u_conn,
441441 char * buf = NULL ;
442442 char * str_method = NULL ;
443443 char * fmt_plain = \
444- "%s %s HTTP/1.%i\r\n"
445- "Host: %s:%i\r\n"
446- "Content-Length: %i\r\n" ;
444+ "%s %s HTTP/1.%i\r\n" ;
447445 char * fmt_proxy = \
448446 "%s http://%s:%i/%s HTTP/1.%i\r\n"
449- "Host: %s:%i\r\n"
450- "Proxy-Connection: KeepAlive\r\n"
451- "Content-Length: %i\r\n" ;
447+ "Proxy-Connection: KeepAlive\r\n" ;
452448
453449 struct flb_http_client * c ;
454- struct flb_upstream * u = u_conn -> u ;
455450
456451 switch (method ) {
457452 case FLB_HTTP_GET :
@@ -481,8 +476,6 @@ struct flb_http_client *flb_http_client(struct flb_upstream_conn *u_conn,
481476 str_method ,
482477 uri ,
483478 flags & FLB_HTTP_10 ? 0 : 1 ,
484- u -> tcp_host ,
485- u -> tcp_port ,
486479 body_len );
487480 }
488481 else {
@@ -493,8 +486,6 @@ struct flb_http_client *flb_http_client(struct flb_upstream_conn *u_conn,
493486 host ,
494487 port ,
495488 "" ,
496- host ,
497- port ,
498489 body_len );
499490 }
500491
@@ -754,13 +745,92 @@ static int http_header_push(struct flb_http_client *c, struct flb_kv *header)
754745 return 0 ;
755746}
756747
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+ }
757768
758769static int http_headers_compose (struct flb_http_client * c )
759770{
760771 int ret ;
772+ int len ;
773+ flb_sds_t tmp ;
774+ flb_sds_t host ;
775+ char * out_host ;
776+ int out_port ;
761777 struct mk_list * head ;
762778 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+ }
763832
833+ /* Push header list to one buffer */
764834 mk_list_foreach (head , & c -> headers ) {
765835 header = mk_list_entry (head , struct flb_kv , _head );
766836 ret = http_header_push (c , header );
0 commit comments