Added mg_forward()

This commit is contained in:
Sergey Lyubka 2014-07-31 20:04:59 +01:00
parent e2fb707d9f
commit cc54195f59
2 changed files with 38 additions and 28 deletions

View File

@ -1297,6 +1297,7 @@ enum endpoint_type {
#define MG_LONG_RUNNING NSF_USER_2 #define MG_LONG_RUNNING NSF_USER_2
#define MG_CGI_CONN NSF_USER_3 #define MG_CGI_CONN NSF_USER_3
#define MG_PROXY_CONN NSF_USER_4 #define MG_PROXY_CONN NSF_USER_4
#define MG_PROXY_DONT_PARSE NSF_USER_5
struct connection { struct connection {
struct ns_connection *ns_conn; // NOTE(lsm): main.c depends on this order struct ns_connection *ns_conn; // NOTE(lsm): main.c depends on this order
@ -4086,12 +4087,38 @@ int mg_terminate_ssl(struct mg_connection *c, const char *cert) {
} }
#endif #endif
int mg_forward(struct mg_connection *c, const char *host, int port, int ssl) {
struct connection *conn = MG_CONN_2_CONN(c);
struct ns_server *server = &conn->server->ns_server;
struct ns_connection *pc;
if ((pc = ns_connect(server, host, port, ssl, conn)) == NULL) return 0;
// Interlink two connections
pc->flags |= MG_PROXY_CONN;
conn->endpoint_type = EP_PROXY;
conn->endpoint.nc = pc;
DBG(("%p [%s] -> %p %p", conn, c->uri, pc, conn->ns_conn->ssl));
if (strcmp(c->request_method, "CONNECT") == 0) {
// For CONNECT request, reply with 200 OK. Tunnel is established.
mg_printf(c, "%s", "HTTP/1.1 200 OK\r\n\r\n");
conn->request_len = 0;
free(conn->request);
conn->request = NULL;
} else {
// Strip "http://host:port" part from the URI
if (memcmp(c->uri, "http://", 7) == 0) c->uri += 7;
while (*c->uri != '\0' && *c->uri != '/') c->uri++;
proxy_request(pc, c);
}
return 1;
}
static void proxify_connection(struct connection *conn) { static void proxify_connection(struct connection *conn) {
char proto[10], host[500], cert[500]; char proto[10], host[500], cert[500];
unsigned short port = 80; unsigned short port = 80;
struct mg_connection *c = &conn->mg_conn; struct mg_connection *c = &conn->mg_conn;
struct ns_server *server = &conn->server->ns_server;
struct ns_connection *pc = NULL;
int n = 0; int n = 0;
const char *url = c->uri; const char *url = c->uri;
@ -4119,26 +4146,7 @@ static void proxify_connection(struct connection *conn) {
} }
#endif #endif
if (n > 0 && if (n > 0 && mg_forward(c, host, port, conn->ns_conn->ssl != NULL)) {
(pc = ns_connect(server, host, port, conn->ns_conn->ssl != NULL,
conn)) != NULL) {
// Interlink two connections
pc->flags |= MG_PROXY_CONN;
conn->endpoint_type = EP_PROXY;
conn->endpoint.nc = pc;
DBG(("%p [%s] -> %p %p", conn, c->uri, pc, conn->ns_conn->ssl));
if (strcmp(c->request_method, "CONNECT") == 0) {
// For CONNECT request, reply with 200 OK. Tunnel is established.
mg_printf(c, "%s", "HTTP/1.1 200 OK\r\n\r\n");
conn->request_len = 0;
free(conn->request);
conn->request = NULL;
} else {
// For other methods, forward the request to the target host.
c->uri += n;
proxy_request(pc, c);
}
} else { } else {
conn->ns_conn->flags |= NSF_CLOSE_IMMEDIATELY; conn->ns_conn->flags |= NSF_CLOSE_IMMEDIATELY;
} }
@ -4335,7 +4343,7 @@ static void try_parse(struct connection *conn) {
} }
static void do_proxy(struct connection *conn) { static void do_proxy(struct connection *conn) {
if (conn->request_len == 0) { if (0 && conn->request_len == 0) {
try_parse(conn); try_parse(conn);
DBG(("%p parsing -> %d", conn, conn->request_len)); DBG(("%p parsing -> %d", conn, conn->request_len));
if (conn->request_len > 0 && call_user(conn, MG_REQUEST) == MG_FALSE) { if (conn->request_len > 0 && call_user(conn, MG_REQUEST) == MG_FALSE) {

View File

@ -130,6 +130,8 @@ int mg_authorize_digest(struct mg_connection *c, FILE *fp);
int mg_url_encode(const char *src, size_t s_len, char *dst, size_t dst_len); int mg_url_encode(const char *src, size_t s_len, char *dst, size_t dst_len);
int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int); int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int);
int mg_terminate_ssl(struct mg_connection *c, const char *cert); int mg_terminate_ssl(struct mg_connection *c, const char *cert);
int mg_forward(struct mg_connection *, const char *host, int port, int use_ssl);
// Templates support // Templates support
struct mg_expansion { struct mg_expansion {