8 #include "http_chunk.h"
10 #include "connections.h"
16 #include "inet_ntop_cache.h"
19 #include <sys/types.h>
31 #include "sys-socket.h"
33 #define data_proxy data_fastcgi
34 #define data_proxy_init data_fastcgi_init
36 #define PROXY_RETRY_TIMEOUT 60
40 * the proxy module is based on the fastcgi module
42 * 28.06.2004 Jan Kneschke The first release
43 * 01.07.2004 Evgeny Rodichev Several bugfixes and cleanups
44 * - co-ordinate up- and downstream flows correctly (proxy_demux_response
45 * and proxy_handle_fdevent)
46 * - correctly transfer upstream http_response_status;
47 * - some unused structures removed.
49 * TODO: - delay upstream read if write_queue is too large
50 * (to prevent memory eating, like in apache). Shoud be
52 * - persistent connection with upstream servers
65 unsigned short replace_http_host
;
67 proxy_balance_t balance
;
73 buffer
*parse_response
;
76 plugin_config
**config_storage
;
84 PROXY_STATE_PREPARE_WRITE
,
87 } proxy_connection_state_t
;
89 enum { PROXY_STDOUT
, PROXY_END_REQUEST
};
92 proxy_connection_state_t state
;
93 time_t state_timestamp
;
98 buffer
*response_header
;
103 int fd
; /* fd to the proxy process */
104 int fde_ndx
; /* index into the fd-event buffer */
108 connection
*remote_conn
; /* dumb pointer */
109 plugin_data
*plugin_data
; /* dumb pointer */
114 /* ok, we need a prototype */
115 static handler_t
proxy_handle_fdevent(server
*srv
, void *ctx
, int revents
);
117 static handler_ctx
* handler_ctx_init(void) {
121 hctx
= calloc(1, sizeof(*hctx
));
123 hctx
->state
= PROXY_STATE_INIT
;
126 hctx
->response
= buffer_init();
127 hctx
->response_header
= buffer_init();
129 hctx
->wb
= chunkqueue_init();
138 static void handler_ctx_free(handler_ctx
*hctx
) {
139 buffer_free(hctx
->response
);
140 buffer_free(hctx
->response_header
);
141 chunkqueue_free(hctx
->wb
);
146 INIT_FUNC(mod_proxy_init
) {
149 p
= calloc(1, sizeof(*p
));
151 p
->parse_response
= buffer_init();
152 p
->balance_buf
= buffer_init();
158 FREE_FUNC(mod_proxy_free
) {
159 plugin_data
*p
= p_d
;
163 buffer_free(p
->parse_response
);
164 buffer_free(p
->balance_buf
);
166 if (p
->config_storage
) {
168 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
169 plugin_config
*s
= p
->config_storage
[i
];
171 if (NULL
== s
) continue;
173 array_free(s
->extensions
);
177 free(p
->config_storage
);
182 return HANDLER_GO_ON
;
185 SETDEFAULTS_FUNC(mod_proxy_set_defaults
) {
186 plugin_data
*p
= p_d
;
190 config_values_t cv
[] = {
191 { "proxy.server", NULL
, T_CONFIG_LOCAL
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
192 { "proxy.debug", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
193 { "proxy.balance", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 2 */
194 { "proxy.replace-http-host", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 3 */
195 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
198 p
->config_storage
= calloc(1, srv
->config_context
->used
* sizeof(plugin_config
*));
200 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
201 data_config
const* config
= (data_config
const*)srv
->config_context
->data
[i
];
204 s
= malloc(sizeof(plugin_config
));
205 s
->extensions
= array_init();
207 s
->replace_http_host
= 0;
209 cv
[0].destination
= s
->extensions
;
210 cv
[1].destination
= &(s
->debug
);
211 cv
[2].destination
= p
->balance_buf
;
212 cv
[3].destination
= &(s
->replace_http_host
);
214 buffer_reset(p
->balance_buf
);
216 p
->config_storage
[i
] = s
;
218 if (0 != config_insert_values_global(srv
, config
->value
, cv
, i
== 0 ? T_CONFIG_SCOPE_SERVER
: T_CONFIG_SCOPE_CONNECTION
)) {
219 return HANDLER_ERROR
;
222 if (buffer_string_is_empty(p
->balance_buf
)) {
223 s
->balance
= PROXY_BALANCE_FAIR
;
224 } else if (buffer_is_equal_string(p
->balance_buf
, CONST_STR_LEN("fair"))) {
225 s
->balance
= PROXY_BALANCE_FAIR
;
226 } else if (buffer_is_equal_string(p
->balance_buf
, CONST_STR_LEN("round-robin"))) {
227 s
->balance
= PROXY_BALANCE_RR
;
228 } else if (buffer_is_equal_string(p
->balance_buf
, CONST_STR_LEN("hash"))) {
229 s
->balance
= PROXY_BALANCE_HASH
;
231 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
232 "proxy.balance has to be one of: fair, round-robin, hash, but not:", p
->balance_buf
);
233 return HANDLER_ERROR
;
236 if (NULL
!= (du
= array_get_element(config
->value
, "proxy.server"))) {
238 data_array
*da
= (data_array
*)du
;
240 if (du
->type
!= TYPE_ARRAY
) {
241 log_error_write(srv
, __FILE__
, __LINE__
, "sss",
242 "unexpected type for key: ", "proxy.server", "expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
244 return HANDLER_ERROR
;
248 * proxy.server = ( "<ext>" => ...,
252 for (j
= 0; j
< da
->value
->used
; j
++) {
253 data_array
*da_ext
= (data_array
*)da
->value
->data
[j
];
256 if (da_ext
->type
!= TYPE_ARRAY
) {
257 log_error_write(srv
, __FILE__
, __LINE__
, "sssbs",
258 "unexpected type for key: ", "proxy.server",
259 "[", da
->value
->data
[j
]->key
, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
261 return HANDLER_ERROR
;
265 * proxy.server = ( "<ext>" =>
266 * ( "<host>" => ( ... ),
267 * "<host>" => ( ... )
272 for (n
= 0; n
< da_ext
->value
->used
; n
++) {
273 data_array
*da_host
= (data_array
*)da_ext
->value
->data
[n
];
278 config_values_t pcv
[] = {
279 { "host", NULL
, T_CONFIG_STRING
, T_CONFIG_SCOPE_CONNECTION
}, /* 0 */
280 { "port", NULL
, T_CONFIG_SHORT
, T_CONFIG_SCOPE_CONNECTION
}, /* 1 */
281 { NULL
, NULL
, T_CONFIG_UNSET
, T_CONFIG_SCOPE_UNSET
}
284 if (da_host
->type
!= TYPE_ARRAY
) {
285 log_error_write(srv
, __FILE__
, __LINE__
, "ssSBS",
286 "unexpected type for key:",
288 "[", da_ext
->value
->data
[n
]->key
, "](string); expected ( \"ext\" => ( \"backend-label\" => ( \"key\" => \"value\" )))");
290 return HANDLER_ERROR
;
293 df
= data_proxy_init();
297 buffer_copy_buffer(df
->key
, da_host
->key
);
299 pcv
[0].destination
= df
->host
;
300 pcv
[1].destination
= &(df
->port
);
302 if (0 != config_insert_values_internal(srv
, da_host
->value
, pcv
, T_CONFIG_SCOPE_CONNECTION
)) {
303 df
->free((data_unset
*) df
);
304 return HANDLER_ERROR
;
307 if (buffer_string_is_empty(df
->host
)) {
308 log_error_write(srv
, __FILE__
, __LINE__
, "sbbbs",
309 "missing key (string):",
315 df
->free((data_unset
*) df
);
316 return HANDLER_ERROR
;
319 /* if extension already exists, take it */
321 if (NULL
== (dfa
= (data_array
*)array_get_element(s
->extensions
, da_ext
->key
->ptr
))) {
322 dfa
= data_array_init();
324 buffer_copy_buffer(dfa
->key
, da_ext
->key
);
326 array_insert_unique(dfa
->value
, (data_unset
*)df
);
327 array_insert_unique(s
->extensions
, (data_unset
*)dfa
);
329 array_insert_unique(dfa
->value
, (data_unset
*)df
);
336 return HANDLER_GO_ON
;
340 static void proxy_backend_close(server
*srv
, handler_ctx
*hctx
) {
341 if (hctx
->fd
!= -1) {
342 fdevent_event_del(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
);
343 fdevent_unregister(srv
->ev
, hctx
->fd
);
344 fdevent_sched_close(srv
->ev
, hctx
->fd
, 1);
355 static data_proxy
* mod_proxy_extension_host_get(server
*srv
, connection
*con
, data_array
*extension
, proxy_balance_t balance
, int debug
) {
356 unsigned long last_max
= ULONG_MAX
;
357 int max_usage
= INT_MAX
;
361 if (extension
->value
->used
== 1) {
362 if ( ((data_proxy
*)extension
->value
->data
[0])->is_disabled
) {
367 } else if (extension
->value
->used
!= 0) switch(balance
) {
368 case PROXY_BALANCE_HASH
:
372 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
373 "proxy - used hash balancing, hosts:", extension
->value
->used
);
376 for (k
= 0, ndx
= -1, last_max
= ULONG_MAX
; k
< extension
->value
->used
; k
++) {
377 data_proxy
*host
= (data_proxy
*)extension
->value
->data
[k
];
378 unsigned long cur_max
;
380 if (host
->is_disabled
) continue;
382 cur_max
= generate_crc32c(CONST_BUF_LEN(con
->uri
.path
)) +
383 generate_crc32c(CONST_BUF_LEN(host
->host
)) + /* we can cache this */
384 generate_crc32c(CONST_BUF_LEN(con
->uri
.authority
));
387 log_error_write(srv
, __FILE__
, __LINE__
, "sbbbd",
395 if ((last_max
== ULONG_MAX
) || /* first round */
396 (cur_max
> last_max
)) {
404 case PROXY_BALANCE_FAIR
:
407 log_error_write(srv
, __FILE__
, __LINE__
, "s",
408 "proxy - used fair balancing");
411 for (k
= 0, ndx
= -1, max_usage
= INT_MAX
; k
< extension
->value
->used
; k
++) {
412 data_proxy
*host
= (data_proxy
*)extension
->value
->data
[k
];
414 if (host
->is_disabled
) continue;
416 if (host
->usage
< max_usage
) {
417 max_usage
= host
->usage
;
424 case PROXY_BALANCE_RR
: {
429 log_error_write(srv
, __FILE__
, __LINE__
, "s",
430 "proxy - used round-robin balancing");
433 /* just to be sure */
434 force_assert(extension
->value
->used
< INT_MAX
);
436 host
= (data_proxy
*)extension
->value
->data
[0];
438 /* Use last_used_ndx from first host in list */
439 k
= host
->last_used_ndx
;
440 ndx
= k
+ 1; /* use next host after the last one */
441 if (ndx
< 0) ndx
= 0;
443 /* Search first active host after last_used_ndx */
444 while ( ndx
< (int) extension
->value
->used
445 && (host
= (data_proxy
*)extension
->value
->data
[ndx
])->is_disabled
) ndx
++;
447 if (ndx
>= (int) extension
->value
->used
) {
448 /* didn't found a higher id, wrap to the start */
449 for (ndx
= 0; ndx
<= (int) k
; ndx
++) {
450 host
= (data_proxy
*)extension
->value
->data
[ndx
];
451 if (!host
->is_disabled
) break;
454 /* No active host found */
455 if (host
->is_disabled
) ndx
= -1;
458 /* Save new index for next round */
459 ((data_proxy
*)extension
->value
->data
[0])->last_used_ndx
= ndx
;
469 data_proxy
*host
= (data_proxy
*)extension
->value
->data
[ndx
];
472 log_error_write(srv
, __FILE__
, __LINE__
, "sbd",
473 "proxy - found a host",
474 host
->host
, host
->port
);
480 /* no handler found */
481 con
->http_status
= 503; /* Service Unavailable */
484 log_error_write(srv
, __FILE__
, __LINE__
, "sb",
485 "no proxy-handler found for:",
492 static void proxy_connection_close(server
*srv
, handler_ctx
*hctx
) {
496 p
= hctx
->plugin_data
;
497 con
= hctx
->remote_conn
;
499 proxy_backend_close(srv
, hctx
);
500 handler_ctx_free(hctx
);
501 con
->plugin_ctx
[p
->id
] = NULL
;
503 /* finish response (if not already con->file_started, con->file_finished) */
504 if (con
->mode
== p
->id
) {
505 http_response_backend_done(srv
, con
);
509 static handler_t
proxy_reconnect(server
*srv
, handler_ctx
*hctx
) {
510 proxy_backend_close(srv
, hctx
);
512 hctx
->host
= mod_proxy_extension_host_get(srv
, hctx
->remote_conn
, hctx
->ext
, hctx
->conf
.balance
, (int)hctx
->conf
.debug
);
513 if (NULL
== hctx
->host
) return HANDLER_FINISHED
;
515 hctx
->state
= PROXY_STATE_INIT
;
516 return HANDLER_COMEBACK
;
519 static int proxy_establish_connection(server
*srv
, handler_ctx
*hctx
) {
520 struct sockaddr
*proxy_addr
;
521 struct sockaddr_in proxy_addr_in
;
522 #if defined(HAVE_SYS_UN_H)
523 struct sockaddr_un proxy_addr_un
;
525 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
526 struct sockaddr_in6 proxy_addr_in6
;
530 data_proxy
*host
= hctx
->host
;
531 int proxy_fd
= hctx
->fd
;
534 #if defined(HAVE_SYS_UN_H)
535 if (strstr(host
->host
->ptr
, "/")) {
536 if (buffer_string_length(host
->host
) + 1 > sizeof(proxy_addr_un
.sun_path
)) {
537 log_error_write(srv
, __FILE__
, __LINE__
, "sB",
538 "ERROR: Unix Domain socket filename too long:",
543 memset(&proxy_addr_un
, 0, sizeof(proxy_addr_un
));
544 proxy_addr_un
.sun_family
= AF_UNIX
;
545 memcpy(proxy_addr_un
.sun_path
, host
->host
->ptr
, buffer_string_length(host
->host
) + 1);
546 servlen
= sizeof(proxy_addr_un
);
547 proxy_addr
= (struct sockaddr
*) &proxy_addr_un
;
550 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
551 if (strstr(host
->host
->ptr
, ":")) {
552 memset(&proxy_addr_in6
, 0, sizeof(proxy_addr_in6
));
553 proxy_addr_in6
.sin6_family
= AF_INET6
;
554 inet_pton(AF_INET6
, host
->host
->ptr
, (char *) &proxy_addr_in6
.sin6_addr
);
555 proxy_addr_in6
.sin6_port
= htons(host
->port
);
556 servlen
= sizeof(proxy_addr_in6
);
557 proxy_addr
= (struct sockaddr
*) &proxy_addr_in6
;
561 memset(&proxy_addr_in
, 0, sizeof(proxy_addr_in
));
562 proxy_addr_in
.sin_family
= AF_INET
;
563 proxy_addr_in
.sin_addr
.s_addr
= inet_addr(host
->host
->ptr
);
564 proxy_addr_in
.sin_port
= htons(host
->port
);
565 servlen
= sizeof(proxy_addr_in
);
566 proxy_addr
= (struct sockaddr
*) &proxy_addr_in
;
570 if (-1 == connect(proxy_fd
, proxy_addr
, servlen
)) {
571 if (errno
== EINPROGRESS
|| errno
== EALREADY
) {
572 if (hctx
->conf
.debug
) {
573 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
574 "connect delayed:", proxy_fd
);
580 log_error_write(srv
, __FILE__
, __LINE__
, "sdsd",
581 "connect failed:", proxy_fd
, strerror(errno
), errno
);
586 if (hctx
->conf
.debug
) {
587 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
588 "connect succeeded: ", proxy_fd
);
594 static void proxy_set_header(connection
*con
, const char *key
, const char *value
) {
597 if (NULL
== (ds_dst
= (data_string
*)array_get_unused_element(con
->request
.headers
, TYPE_STRING
))) {
598 ds_dst
= data_string_init();
601 buffer_copy_string(ds_dst
->key
, key
);
602 buffer_copy_string(ds_dst
->value
, value
);
603 array_insert_unique(con
->request
.headers
, (data_unset
*)ds_dst
);
606 static void proxy_append_header(connection
*con
, const char *key
, const char *value
) {
609 if (NULL
== (ds_dst
= (data_string
*)array_get_unused_element(con
->request
.headers
, TYPE_STRING
))) {
610 ds_dst
= data_string_init();
613 buffer_copy_string(ds_dst
->key
, key
);
614 buffer_append_string(ds_dst
->value
, value
);
615 array_insert_unique(con
->request
.headers
, (data_unset
*)ds_dst
);
619 static int proxy_create_env(server
*srv
, handler_ctx
*hctx
) {
622 connection
*con
= hctx
->remote_conn
;
624 int replace_http_host
= 0;
631 buffer_copy_string(b
, get_http_method_name(con
->request
.http_method
));
632 buffer_append_string_len(b
, CONST_STR_LEN(" "));
634 buffer_append_string_buffer(b
, con
->request
.uri
);
635 buffer_append_string_len(b
, CONST_STR_LEN(" HTTP/1.0\r\n"));
636 if (hctx
->conf
.replace_http_host
&& !buffer_string_is_empty(hctx
->host
->key
)) {
637 replace_http_host
= 1;
638 if (hctx
->conf
.debug
> 1) {
639 log_error_write(srv
, __FILE__
, __LINE__
, "SBS",
640 "proxy - using \"", hctx
->host
->key
, "\" as HTTP Host");
642 buffer_append_string_len(b
, CONST_STR_LEN("Host: "));
643 buffer_append_string_buffer(b
, hctx
->host
->key
);
644 buffer_append_string_len(b
, CONST_STR_LEN("\r\n"));
647 proxy_append_header(con
, "X-Forwarded-For", (char *)inet_ntop_cache_get_ip(srv
, &(con
->dst_addr
)));
648 /* http_host is NOT is just a pointer to a buffer
649 * which is NULL if it is not set */
650 if (!buffer_string_is_empty(con
->request
.http_host
)) {
651 proxy_set_header(con
, "X-Host", con
->request
.http_host
->ptr
);
653 proxy_set_header(con
, "X-Forwarded-Proto", con
->uri
.scheme
->ptr
);
656 for (i
= 0; i
< con
->request
.headers
->used
; i
++) {
659 ds
= (data_string
*)con
->request
.headers
->data
[i
];
661 if (!buffer_is_empty(ds
->value
) && !buffer_is_empty(ds
->key
)) {
662 if (replace_http_host
&&
663 buffer_is_equal_caseless_string(ds
->key
, CONST_STR_LEN("Host"))) continue;
664 if (buffer_is_equal_caseless_string(ds
->key
, CONST_STR_LEN("Connection"))) continue;
665 if (buffer_is_equal_caseless_string(ds
->key
, CONST_STR_LEN("Proxy-Connection"))) continue;
666 /* Do not emit HTTP_PROXY in environment.
667 * Some executables use HTTP_PROXY to configure
668 * outgoing proxy. See also https://httpoxy.org/ */
669 if (buffer_is_equal_caseless_string(ds
->key
, CONST_STR_LEN("Proxy"))) continue;
671 buffer_append_string_buffer(b
, ds
->key
);
672 buffer_append_string_len(b
, CONST_STR_LEN(": "));
673 buffer_append_string_buffer(b
, ds
->value
);
674 buffer_append_string_len(b
, CONST_STR_LEN("\r\n"));
678 buffer_append_string_len(b
, CONST_STR_LEN("Connection: close\r\n\r\n"));
680 hctx
->wb_reqlen
= buffer_string_length(b
);
681 chunkqueue_append_buffer(hctx
->wb
, b
);
686 if (con
->request
.content_length
) {
687 chunkqueue_append_chunkqueue(hctx
->wb
, con
->request_content_queue
);
688 hctx
->wb_reqlen
+= con
->request
.content_length
;/* (eventual) total request size */
694 static int proxy_set_state(server
*srv
, handler_ctx
*hctx
, proxy_connection_state_t state
) {
696 hctx
->state_timestamp
= srv
->cur_ts
;
702 static int proxy_response_parse(server
*srv
, connection
*con
, plugin_data
*p
, buffer
*in
) {
704 int http_response_status
= -1;
708 /* [\r]\n -> [\0]\0 */
710 buffer_copy_buffer(p
->parse_response
, in
);
712 for (s
= p
->parse_response
->ptr
; NULL
!= (ns
= strchr(s
, '\n')); s
= ns
+ 1) {
719 if (s
!= ns
&& ns
[-1] == '\r') ns
[-1] = '\0';
721 if (-1 == http_response_status
) {
722 /* The first line of a Response message is the Status-Line */
724 for (key
=s
; *key
&& *key
!= ' '; key
++);
727 http_response_status
= (int) strtol(key
, NULL
, 10);
728 if (http_response_status
< 100 || http_response_status
>= 1000) http_response_status
= 502;
730 http_response_status
= 502;
733 con
->http_status
= http_response_status
;
734 con
->parsed_response
|= HTTP_STATUS
;
738 if (NULL
== (value
= strchr(s
, ':'))) {
739 /* now we expect: "<key>: <value>\n" */
745 key_len
= value
- key
;
749 while (*value
== ' ' || *value
== '\t') value
++;
755 if (0 == strncasecmp(key
, "Date", key_len
)) {
756 con
->parsed_response
|= HTTP_DATE
;
760 if (0 == strncasecmp(key
, "Location", key_len
)) {
761 con
->parsed_response
|= HTTP_LOCATION
;
765 if (0 == strncasecmp(key
, "Connection", key_len
)) {
770 if (0 == strncasecmp(key
, "Content-Length", key_len
)) {
771 con
->response
.content_length
= strtoul(value
, NULL
, 10);
772 con
->parsed_response
|= HTTP_CONTENT_LENGTH
;
780 if (NULL
== (ds
= (data_string
*)array_get_unused_element(con
->response
.headers
, TYPE_STRING
))) {
781 ds
= data_response_init();
783 buffer_copy_string_len(ds
->key
, key
, key_len
);
784 buffer_copy_string(ds
->value
, value
);
786 array_insert_unique(con
->response
.headers
, (data_unset
*)ds
);
794 static int proxy_demux_response(server
*srv
, handler_ctx
*hctx
) {
799 plugin_data
*p
= hctx
->plugin_data
;
800 connection
*con
= hctx
->remote_conn
;
801 int proxy_fd
= hctx
->fd
;
803 /* check how much we have to read */
804 #if !defined(_WIN32) && !defined(__CYGWIN__)
805 if (ioctl(hctx
->fd
, FIONREAD
, &b
)) {
806 if (errno
== EAGAIN
) {
807 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
810 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
820 if (hctx
->conf
.debug
) {
821 log_error_write(srv
, __FILE__
, __LINE__
, "sd",
822 "proxy - have to read:", b
);
826 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)) {
827 off_t cqlen
= chunkqueue_length(con
->write_queue
);
828 if (cqlen
+ b
> 65536 - 4096) {
829 if (!con
->is_writable
) {
830 /*(defer removal of FDEVENT_IN interest since
831 * connection_state_machine() might be able to send data
832 * immediately, unless !con->is_writable, where
833 * connection_state_machine() might not loop back to call
834 * mod_proxy_handle_subrequest())*/
835 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
837 if (cqlen
>= 65536-1) return 0;
838 b
= 65536 - 1 - (int)cqlen
;
842 buffer_string_prepare_append(hctx
->response
, b
);
844 if (-1 == (r
= read(hctx
->fd
, hctx
->response
->ptr
+ buffer_string_length(hctx
->response
), buffer_string_space(hctx
->response
)))) {
845 if (errno
== EAGAIN
) {
846 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
849 log_error_write(srv
, __FILE__
, __LINE__
, "sds",
850 "unexpected end-of-file (perhaps the proxy process died):",
851 proxy_fd
, strerror(errno
));
855 #if defined(_WIN32) || defined(__CYGWIN__)
856 if (0 == r
) return 1; /* fin */
859 /* this should be catched by the b > 0 above */
862 buffer_commit(hctx
->response
, r
);
865 log_error_write(srv
, __FILE__
, __LINE__
, "sdsbs",
866 "demux: Response buffer len", hctx
->response
->used
, ":", hctx
->response
, ":");
869 if (0 == con
->got_response
) {
870 con
->got_response
= 1;
871 buffer_string_prepare_copy(hctx
->response_header
, 1023);
874 if (0 == con
->file_started
) {
877 /* search for the \r\n\r\n in the string */
878 if (NULL
!= (c
= buffer_search_string_len(hctx
->response
, CONST_STR_LEN("\r\n\r\n")))) {
879 size_t hlen
= c
- hctx
->response
->ptr
+ 4;
880 size_t blen
= buffer_string_length(hctx
->response
) - hlen
;
883 buffer_append_string_len(hctx
->response_header
, hctx
->response
->ptr
, hlen
);
885 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "Header:", hctx
->response_header
);
887 /* parse the response header */
888 proxy_response_parse(srv
, con
, p
, hctx
->response_header
);
890 con
->file_started
= 1;
892 if (0 != http_chunk_append_mem(srv
, con
, c
+ 4, blen
)) {
893 /* error writing to tempfile;
894 * truncate response or send 500 if nothing sent yet */
896 con
->file_started
= 0;
899 buffer_reset(hctx
->response
);
901 /* no luck, no header found */
902 /*(reuse MAX_HTTP_REQUEST_HEADER as max size for response headers from backends)*/
903 if (buffer_string_length(hctx
->response
) > MAX_HTTP_REQUEST_HEADER
) {
904 log_error_write(srv
, __FILE__
, __LINE__
, "sb", "response headers too large for", con
->uri
.path
);
905 con
->http_status
= 502; /* Bad Gateway */
911 if (0 != http_chunk_append_buffer(srv
, con
, hctx
->response
)) {
912 /* error writing to tempfile;
913 * truncate response or send 500 if nothing sent yet */
916 buffer_reset(hctx
->response
);
919 /* reading from upstream done */
927 static handler_t
proxy_write_request(server
*srv
, handler_ctx
*hctx
) {
928 data_proxy
*host
= hctx
->host
;
929 connection
*con
= hctx
->remote_conn
;
933 switch(hctx
->state
) {
934 case PROXY_STATE_INIT
:
935 #if defined(HAVE_SYS_UN_H)
936 if (strstr(host
->host
->ptr
,"/")) {
937 if (-1 == (hctx
->fd
= fdevent_socket_nb_cloexec(AF_UNIX
, SOCK_STREAM
, 0))) {
938 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "socket failed: ", strerror(errno
));
939 return HANDLER_ERROR
;
943 #if defined(HAVE_IPV6) && defined(HAVE_INET_PTON)
944 if (strstr(host
->host
->ptr
,":")) {
945 if (-1 == (hctx
->fd
= fdevent_socket_nb_cloexec(AF_INET6
, SOCK_STREAM
, 0))) {
946 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "socket failed: ", strerror(errno
));
947 return HANDLER_ERROR
;
952 if (-1 == (hctx
->fd
= fdevent_socket_nb_cloexec(AF_INET
, SOCK_STREAM
, 0))) {
953 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "socket failed: ", strerror(errno
));
954 return HANDLER_ERROR
;
961 fdevent_register(srv
->ev
, hctx
->fd
, proxy_handle_fdevent
, hctx
);
963 if (-1 == fdevent_fcntl_set(srv
->ev
, hctx
->fd
)) {
964 log_error_write(srv
, __FILE__
, __LINE__
, "ss", "fcntl failed: ", strerror(errno
));
966 return HANDLER_ERROR
;
970 case PROXY_STATE_CONNECT
:
971 if (hctx
->state
== PROXY_STATE_INIT
) {
972 switch (proxy_establish_connection(srv
, hctx
)) {
974 proxy_set_state(srv
, hctx
, PROXY_STATE_CONNECT
);
976 /* connection is in progress, wait for an event and call getsockopt() below */
978 fdevent_event_set(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
980 return HANDLER_WAIT_FOR_EVENT
;
982 /* if ECONNREFUSED choose another connection */
985 return HANDLER_ERROR
;
987 /* everything is ok, go on */
992 socklen_t socket_error_len
= sizeof(socket_error
);
994 /* try to finish the connect() */
995 if (0 != getsockopt(hctx
->fd
, SOL_SOCKET
, SO_ERROR
, &socket_error
, &socket_error_len
)) {
996 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
997 "getsockopt failed:", strerror(errno
));
999 return HANDLER_ERROR
;
1001 if (socket_error
!= 0) {
1002 log_error_write(srv
, __FILE__
, __LINE__
, "ss",
1003 "establishing connection failed:", strerror(socket_error
),
1004 "port:", hctx
->host
->port
);
1006 return HANDLER_ERROR
;
1008 if (hctx
->conf
.debug
) {
1009 log_error_write(srv
, __FILE__
, __LINE__
, "s", "proxy - connect - delayed success");
1013 /* ok, we have the connection */
1015 proxy_set_state(srv
, hctx
, PROXY_STATE_PREPARE_WRITE
);
1017 case PROXY_STATE_PREPARE_WRITE
:
1018 proxy_create_env(srv
, hctx
);
1020 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
1021 proxy_set_state(srv
, hctx
, PROXY_STATE_WRITE
);
1024 case PROXY_STATE_WRITE
:;
1025 ret
= srv
->network_backend_write(srv
, con
, hctx
->fd
, hctx
->wb
, MAX_WRITE_LIMIT
);
1027 chunkqueue_remove_finished_chunks(hctx
->wb
);
1029 if (-1 == ret
) { /* error on our side */
1030 log_error_write(srv
, __FILE__
, __LINE__
, "ssd", "write failed:", strerror(errno
), errno
);
1032 return HANDLER_ERROR
;
1033 } else if (-2 == ret
) { /* remote close */
1034 log_error_write(srv
, __FILE__
, __LINE__
, "ssd", "write failed, remote connection close:", strerror(errno
), errno
);
1036 return HANDLER_ERROR
;
1039 if (hctx
->wb
->bytes_out
== hctx
->wb_reqlen
) {
1040 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
1041 proxy_set_state(srv
, hctx
, PROXY_STATE_READ
);
1043 off_t wblen
= hctx
->wb
->bytes_in
- hctx
->wb
->bytes_out
;
1044 if (hctx
->wb
->bytes_in
< hctx
->wb_reqlen
&& wblen
< 65536 - 16384) {
1045 /*(con->conf.stream_request_body & FDEVENT_STREAM_REQUEST)*/
1046 if (!(con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_POLLIN
)) {
1047 con
->conf
.stream_request_body
|= FDEVENT_STREAM_REQUEST_POLLIN
;
1048 con
->is_readable
= 1; /* trigger optimistic read from client */
1052 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
1054 fdevent_event_add(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_OUT
);
1058 return HANDLER_WAIT_FOR_EVENT
;
1059 case PROXY_STATE_READ
:
1060 /* waiting for a response */
1061 return HANDLER_WAIT_FOR_EVENT
;
1063 log_error_write(srv
, __FILE__
, __LINE__
, "s", "(debug) unknown state");
1064 return HANDLER_ERROR
;
1070 static int mod_proxy_patch_connection(server
*srv
, connection
*con
, plugin_data
*p
) {
1072 plugin_config
*s
= p
->config_storage
[0];
1077 PATCH(replace_http_host
);
1079 /* skip the first, the global context */
1080 for (i
= 1; i
< srv
->config_context
->used
; i
++) {
1081 data_config
*dc
= (data_config
*)srv
->config_context
->data
[i
];
1082 s
= p
->config_storage
[i
];
1084 /* condition didn't match */
1085 if (!config_check_cond(srv
, con
, dc
)) continue;
1088 for (j
= 0; j
< dc
->value
->used
; j
++) {
1089 data_unset
*du
= dc
->value
->data
[j
];
1091 if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("proxy.server"))) {
1093 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("proxy.debug"))) {
1095 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("proxy.balance"))) {
1097 } else if (buffer_is_equal_string(du
->key
, CONST_STR_LEN("proxy.replace-http-host"))) {
1098 PATCH(replace_http_host
);
1107 static handler_t
proxy_send_request(server
*srv
, handler_ctx
*hctx
) {
1108 /* ok, create the request */
1109 handler_t rc
= proxy_write_request(srv
, hctx
);
1110 if (HANDLER_ERROR
!= rc
) {
1113 data_proxy
*host
= hctx
->host
;
1114 log_error_write(srv
, __FILE__
, __LINE__
, "sbdd", "proxy-server disabled:",
1119 /* disable this server */
1120 host
->is_disabled
= 1;
1121 host
->disable_ts
= srv
->cur_ts
;
1123 /* reset the environment and restart the sub-request */
1124 return proxy_reconnect(srv
, hctx
);
1129 static handler_t
proxy_recv_response(server
*srv
, handler_ctx
*hctx
);
1132 SUBREQUEST_FUNC(mod_proxy_handle_subrequest
) {
1133 plugin_data
*p
= p_d
;
1135 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
1137 if (NULL
== hctx
) return HANDLER_GO_ON
;
1140 if (con
->mode
!= p
->id
) return HANDLER_GO_ON
;
1142 if ((con
->conf
.stream_response_body
& FDEVENT_STREAM_RESPONSE_BUFMIN
)
1143 && con
->file_started
) {
1144 if (chunkqueue_length(con
->write_queue
) > 65536 - 4096) {
1145 fdevent_event_clr(srv
->ev
, &(hctx
->fde_ndx
), hctx
->fd
, FDEVENT_IN
);
1146 } else if (!(fdevent_event_get_interest(srv
->ev
, hctx
->fd
) & FDEVENT_IN
)) {
1147 /* optimistic read from backend, which might re-enable FDEVENT_IN */
1148 handler_t rc
= proxy_recv_response(srv
, hctx
); /*(might invalidate hctx)*/
1149 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
1153 if (0 == hctx
->wb
->bytes_in
1154 ? con
->state
== CON_STATE_READ_POST
1155 : hctx
->wb
->bytes_in
< hctx
->wb_reqlen
) {
1156 /*(64k - 4k to attempt to avoid temporary files
1157 * in conjunction with FDEVENT_STREAM_REQUEST_BUFMIN)*/
1158 if (hctx
->wb
->bytes_in
- hctx
->wb
->bytes_out
> 65536 - 4096
1159 && (con
->conf
.stream_request_body
& FDEVENT_STREAM_REQUEST_BUFMIN
)){
1160 con
->conf
.stream_request_body
&= ~FDEVENT_STREAM_REQUEST_POLLIN
;
1161 if (0 != hctx
->wb
->bytes_in
) return HANDLER_WAIT_FOR_EVENT
;
1163 handler_t r
= connection_handle_read_post_state(srv
, con
);
1164 chunkqueue
*req_cq
= con
->request_content_queue
;
1165 if (0 != hctx
->wb
->bytes_in
&& !chunkqueue_is_empty(req_cq
)) {
1166 chunkqueue_append_chunkqueue(hctx
->wb
, req_cq
);
1167 if (fdevent_event_get_interest(srv
->ev
, hctx
->fd
) & FDEVENT_OUT
) {
1168 return (r
== HANDLER_GO_ON
) ? HANDLER_WAIT_FOR_EVENT
: r
;
1171 if (r
!= HANDLER_GO_ON
) return r
;
1175 return ((0 == hctx
->wb
->bytes_in
|| !chunkqueue_is_empty(hctx
->wb
))
1176 && hctx
->state
!= PROXY_STATE_CONNECT
)
1177 ? proxy_send_request(srv
, hctx
)
1178 : HANDLER_WAIT_FOR_EVENT
;
1182 static handler_t
proxy_recv_response(server
*srv
, handler_ctx
*hctx
) {
1184 switch (proxy_demux_response(srv
, hctx
)) {
1188 http_response_backend_error(srv
, hctx
->remote_conn
);
1192 proxy_connection_close(srv
, hctx
);
1194 return HANDLER_FINISHED
;
1197 return HANDLER_GO_ON
;
1201 static handler_t
proxy_handle_fdevent(server
*srv
, void *ctx
, int revents
) {
1202 handler_ctx
*hctx
= ctx
;
1203 connection
*con
= hctx
->remote_conn
;
1205 joblist_append(srv
, con
);
1207 if (revents
& FDEVENT_IN
) {
1208 handler_t rc
= proxy_recv_response(srv
,hctx
);/*(might invalidate hctx)*/
1209 if (rc
!= HANDLER_GO_ON
) return rc
; /*(unless HANDLER_GO_ON)*/
1212 if (revents
& FDEVENT_OUT
) {
1213 return proxy_send_request(srv
, hctx
); /*(might invalidate hctx)*/
1216 /* perhaps this issue is already handled */
1217 if (revents
& FDEVENT_HUP
) {
1218 if (hctx
->state
== PROXY_STATE_CONNECT
) {
1219 /* connect() -> EINPROGRESS -> HUP */
1220 proxy_send_request(srv
, hctx
); /*(might invalidate hctx)*/
1221 } else if (con
->file_started
) {
1222 /* drain any remaining data from kernel pipe buffers
1223 * even if (con->conf.stream_response_body
1224 * & FDEVENT_STREAM_RESPONSE_BUFMIN)
1225 * since event loop will spin on fd FDEVENT_HUP event
1226 * until unregistered. */
1229 rc
= proxy_recv_response(srv
,hctx
);/*(might invalidate hctx)*/
1230 } while (rc
== HANDLER_GO_ON
); /*(unless HANDLER_GO_ON)*/
1231 return rc
; /* HANDLER_FINISHED or HANDLER_ERROR */
1233 proxy_connection_close(srv
, hctx
);
1235 } else if (revents
& FDEVENT_ERR
) {
1236 log_error_write(srv
, __FILE__
, __LINE__
, "sd", "proxy-FDEVENT_ERR, but no HUP", revents
);
1238 http_response_backend_error(srv
, con
);
1239 proxy_connection_close(srv
, hctx
);
1242 return HANDLER_FINISHED
;
1245 static handler_t
mod_proxy_check_extension(server
*srv
, connection
*con
, void *p_d
) {
1246 plugin_data
*p
= p_d
;
1250 data_array
*extension
= NULL
;
1253 if (con
->mode
!= DIRECT
) return HANDLER_GO_ON
;
1255 /* Possibly, we processed already this request */
1256 if (con
->file_started
== 1) return HANDLER_GO_ON
;
1258 mod_proxy_patch_connection(srv
, con
, p
);
1261 if (buffer_string_is_empty(fn
)) return HANDLER_ERROR
;
1262 s_len
= buffer_string_length(fn
);
1264 /* check if extension matches */
1265 for (k
= 0; k
< p
->conf
.extensions
->used
; k
++) {
1266 data_array
*ext
= NULL
;
1269 ext
= (data_array
*)p
->conf
.extensions
->data
[k
];
1271 if (buffer_is_empty(ext
->key
)) continue;
1273 ct_len
= buffer_string_length(ext
->key
);
1275 if (s_len
< ct_len
) continue;
1277 /* check extension in the form "/proxy_pattern" */
1278 if (*(ext
->key
->ptr
) == '/') {
1279 if (strncmp(fn
->ptr
, ext
->key
->ptr
, ct_len
) == 0) {
1283 } else if (0 == strncmp(fn
->ptr
+ s_len
- ct_len
, ext
->key
->ptr
, ct_len
)) {
1284 /* check extension in the form ".fcg" */
1290 if (NULL
== extension
) {
1291 return HANDLER_GO_ON
;
1294 host
= mod_proxy_extension_host_get(srv
, con
, extension
, p
->conf
.balance
, (int)p
->conf
.debug
);
1296 return HANDLER_FINISHED
;
1299 /* found a server */
1303 * if check-local is disabled, use the uri.path handler
1307 /* init handler-context */
1309 hctx
= handler_ctx_init();
1311 hctx
->remote_conn
= con
;
1312 hctx
->plugin_data
= p
;
1314 hctx
->ext
= extension
;
1316 hctx
->conf
.balance
= p
->conf
.balance
;
1317 hctx
->conf
.debug
= p
->conf
.debug
;
1318 hctx
->conf
.replace_http_host
= p
->conf
.replace_http_host
;
1320 con
->plugin_ctx
[p
->id
] = hctx
;
1323 if (p
->conf
.debug
) {
1324 log_error_write(srv
, __FILE__
, __LINE__
, "sbd",
1325 "proxy - found a host",
1326 host
->host
, host
->port
);
1329 return HANDLER_GO_ON
;
1333 static handler_t
mod_proxy_connection_reset(server
*srv
, connection
*con
, void *p_d
) {
1334 plugin_data
*p
= p_d
;
1335 handler_ctx
*hctx
= con
->plugin_ctx
[p
->id
];
1336 if (hctx
) proxy_connection_close(srv
, hctx
);
1338 return HANDLER_GO_ON
;
1343 * the trigger re-enables the disabled connections after the timeout is over
1347 TRIGGER_FUNC(mod_proxy_trigger
) {
1348 plugin_data
*p
= p_d
;
1350 if (p
->config_storage
) {
1352 for (i
= 0; i
< srv
->config_context
->used
; i
++) {
1353 plugin_config
*s
= p
->config_storage
[i
];
1357 /* get the extensions for all configs */
1359 for (k
= 0; k
< s
->extensions
->used
; k
++) {
1360 data_array
*extension
= (data_array
*)s
->extensions
->data
[k
];
1363 for (n
= 0; n
< extension
->value
->used
; n
++) {
1364 data_proxy
*host
= (data_proxy
*)extension
->value
->data
[n
];
1366 if (!host
->is_disabled
||
1367 srv
->cur_ts
- host
->disable_ts
< 5) continue;
1369 log_error_write(srv
, __FILE__
, __LINE__
, "sbd",
1370 "proxy - re-enabled:",
1371 host
->host
, host
->port
);
1373 host
->is_disabled
= 0;
1379 return HANDLER_GO_ON
;
1383 int mod_proxy_plugin_init(plugin
*p
);
1384 int mod_proxy_plugin_init(plugin
*p
) {
1385 p
->version
= LIGHTTPD_VERSION_ID
;
1386 p
->name
= buffer_init_string("proxy");
1388 p
->init
= mod_proxy_init
;
1389 p
->cleanup
= mod_proxy_free
;
1390 p
->set_defaults
= mod_proxy_set_defaults
;
1391 p
->connection_reset
= mod_proxy_connection_reset
; /* end of req-resp cycle */
1392 p
->handle_connection_close
= mod_proxy_connection_reset
; /* end of client connection */
1393 p
->handle_uri_clean
= mod_proxy_check_extension
;
1394 p
->handle_subrequest
= mod_proxy_handle_subrequest
;
1395 p
->handle_trigger
= mod_proxy_trigger
;