From 2204991274ff67f80cfd592f8470cdaca7a906e9 Mon Sep 17 00:00:00 2001 From: reyk Date: Sat, 2 Aug 2014 11:52:00 +0000 Subject: [PATCH] Allow to specify a FastCGI TCP socket on localhost (eg. :9000). Used for debugging, you should prefer local UNIX sockets, but it helped to find an issue that will be fixed with the next commit. OK florian@ --- usr.sbin/httpd/httpd.h | 4 ++- usr.sbin/httpd/server.c | 23 ++++++++++++++- usr.sbin/httpd/server_fcgi.c | 55 +++++++++++++++++++++++++----------- 3 files changed, 64 insertions(+), 18 deletions(-) diff --git a/usr.sbin/httpd/httpd.h b/usr.sbin/httpd/httpd.h index 30b312d3fbf..10a7d54409f 100644 --- a/usr.sbin/httpd/httpd.h +++ b/usr.sbin/httpd/httpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: httpd.h,v 1.30 2014/08/01 22:24:05 reyk Exp $ */ +/* $OpenBSD: httpd.h,v 1.31 2014/08/02 11:52:00 reyk Exp $ */ /* * Copyright (c) 2006 - 2014 Reyk Floeter @@ -405,6 +405,8 @@ void server_purge(struct server *); int server_socket_af(struct sockaddr_storage *, in_port_t); in_port_t server_socket_getport(struct sockaddr_storage *); +int server_socket_connect(struct sockaddr_storage *, in_port_t, + struct server_config *); void server_write(struct bufferevent *, void *); void server_read(struct bufferevent *, void *); void server_error(struct bufferevent *, short, void *); diff --git a/usr.sbin/httpd/server.c b/usr.sbin/httpd/server.c index 0a2077e27ee..173f6325b5a 100644 --- a/usr.sbin/httpd/server.c +++ b/usr.sbin/httpd/server.c @@ -1,4 +1,4 @@ -/* $OpenBSD: server.c,v 1.21 2014/08/01 22:24:05 reyk Exp $ */ +/* $OpenBSD: server.c,v 1.22 2014/08/02 11:52:01 reyk Exp $ */ /* * Copyright (c) 2006 - 2014 Reyk Floeter @@ -342,6 +342,27 @@ server_socket_listen(struct sockaddr_storage *ss, in_port_t port, return (-1); } +int +server_socket_connect(struct sockaddr_storage *ss, in_port_t port, + struct server_config *srv_conf) +{ + int s; + + if ((s = server_socket(ss, port, srv_conf, -1, 0)) == -1) + return (-1); + + if (connect(s, (struct sockaddr *)ss, ss->ss_len) == -1) { + if (errno != EINPROGRESS) + goto bad; + } + + return (s); + + bad: + close(s); + return (-1); +} + void server_input(struct client *clt) { diff --git a/usr.sbin/httpd/server_fcgi.c b/usr.sbin/httpd/server_fcgi.c index 618d159734f..a9232a89fb2 100644 --- a/usr.sbin/httpd/server_fcgi.c +++ b/usr.sbin/httpd/server_fcgi.c @@ -1,4 +1,4 @@ -/* $OpenBSD: server_fcgi.c,v 1.8 2014/08/02 09:54:13 reyk Exp $ */ +/* $OpenBSD: server_fcgi.c,v 1.9 2014/08/02 11:52:01 reyk Exp $ */ /* * Copyright (c) 2014 Florian Obser @@ -98,26 +98,49 @@ server_fcgi(struct httpd *env, struct client *clt) struct fcgi_begin_request_body *begin; struct kv *kv, key; size_t len; - int fd, total_len; + int fd = -1, total_len; const char *errstr = NULL; - char *request_uri; + char *request_uri, *p; + in_port_t port; + struct sockaddr_storage ss; - if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) - goto fail; + if (srv_conf->path[0] == ':') { + p = srv_conf->path + 1; - bzero(&sun, sizeof(sun)); - bzero(&hbuf, sizeof(hbuf)); - sun.sun_family = AF_UNIX; - len = strlcpy(sun.sun_path, srv_conf->path, sizeof(sun.sun_path)); - if (len >= sizeof(sun.sun_path)) { - errstr = "socket path to long"; - goto fail; + port = strtonum(p, 0, 0xffff, &errstr); + if (errstr != NULL) { + log_warn("%s: strtonum %s, %s", __func__, p, errstr); + goto fail; + } + memset(&ss, 0, sizeof(ss)); + ss.ss_family = AF_INET; + ((struct sockaddr_in *) + &ss)->sin_addr.s_addr = htonl(INADDR_LOOPBACK); + port = htons(port); + + if ((fd = server_socket_connect(&ss, port, srv_conf)) == -1) + goto fail; + } else { + if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) + goto fail; + + memset(&sun, 0, sizeof(sun)); + sun.sun_family = AF_UNIX; + len = strlcpy(sun.sun_path, + srv_conf->path, sizeof(sun.sun_path)); + if (len >= sizeof(sun.sun_path)) { + errstr = "socket path to long"; + goto fail; + } + sun.sun_len = len; + + if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) + goto fail; } - sun.sun_len = len; - if (connect(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) - goto fail; + socket_set_blockmode(fd, BM_NONBLOCK); + memset(&hbuf, 0, sizeof(hbuf)); clt->clt_fcgi_state = FCGI_READ_HEADER; clt->clt_fcgi_toread = sizeof(struct fcgi_record_header); @@ -140,7 +163,7 @@ server_fcgi(struct httpd *env, struct client *clt) goto fail; } - bzero(&buf, sizeof(buf)); + memset(&buf, 0, sizeof(buf)); h = (struct fcgi_record_header *) &buf; h->version = 1; -- 2.20.1