Browse Source

separate http-related code module

mom040267 11 years ago
parent
commit
d2922f53fc
4 changed files with 68 additions and 29 deletions
  1. 1 1
      Makefile.in
  2. 58 0
      src/apps/relay/http_server.c
  3. 5 0
      src/server/ns_turn_ioalib.h
  4. 4 28
      src/server/ns_turn_server.c

+ 1 - 1
Makefile.in

@@ -21,7 +21,7 @@ COMMON_MODS = src/apps/common/apputils.c src/apps/common/ns_turn_utils.c src/app
 COMMON_DEPS = ${LIBCLIENTTURN_DEPS} ${COMMON_MODS} ${COMMON_HEADERS}
 
 IMPL_HEADERS = src/apps/relay/ns_ioalib_impl.h src/apps/relay/ns_sm.h src/apps/relay/turn_ports.h
-IMPL_MODS = src/apps/relay/ns_ioalib_engine_impl.c src/apps/relay/turn_ports.c
+IMPL_MODS = src/apps/relay/ns_ioalib_engine_impl.c src/apps/relay/turn_ports.c src/apps/relay/http_server.c
 IMPL_DEPS = ${COMMON_DEPS} ${IMPL_HEADERS} ${IMPL_MODS}
 
 HIREDIS_HEADERS = src/apps/common/hiredis_libevent2.h

+ 58 - 0
src/apps/relay/http_server.c

@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2011, 2012, 2013 Citrix Systems
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the project nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "ns_ioalib_impl.h"
+
+void write_http_echo(ts_ur_super_session *ss)
+{
+	if(ss && ss->client_socket && !(ss->to_be_closed)) {
+		turn_turnserver *server = (turn_turnserver *)ss->server;
+		if(server) {
+			ioa_network_buffer_handle nbh_http = ioa_network_buffer_allocate(server->e);
+			size_t len_http = ioa_network_buffer_get_size(nbh_http);
+			u08bits *data = ioa_network_buffer_data(nbh_http);
+			char data_http[1025];
+			char content_http[1025];
+			const char* title = "TURN Server";
+			snprintf(content_http,sizeof(content_http)-1,"<!DOCTYPE html>\r\n<html>\r\n  <head>\r\n    <title>%s</title>\r\n  </head>\r\n  <body>\r\n    %s\r\n  </body>\r\n</html>\r\n",title,title);
+			snprintf(data_http,sizeof(data_http)-1,"HTTP/1.1 200 OK\r\nServer: %s\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: %d\r\n\r\n%s",TURN_SOFTWARE,(int)strlen(content_http),content_http);
+			len_http = strlen(data_http);
+			ns_bcopy(data_http,data,len_http);
+			ioa_network_buffer_set_size(nbh_http,len_http);
+			send_data_from_ioa_socket_nbh(ss->client_socket, NULL, nbh_http, TTL_IGNORE, TOS_IGNORE);
+		}
+	}
+}
+
+void handle_https(ts_ur_super_session *ss, ioa_network_buffer_handle nbh) {
+	//TODO
+	UNUSED_ARG(nbh);
+	write_http_echo(ss);
+}

+ 5 - 0
src/server/ns_turn_ioalib.h

@@ -276,6 +276,11 @@ void get_realm_options_by_name(char *realm, realm_options_t* ro);
 int get_canonic_origin(const char* o, char *co, int sz);
 int get_default_protocol_port(const char* scheme, size_t slen);
 
+///////////// HTTP ////////////////////
+
+void write_http_echo(ts_ur_super_session *ss);
+void handle_https(ts_ur_super_session *ss, ioa_network_buffer_handle nbh);
+
 ///////////////////////////////////////
 
 #ifdef __cplusplus

+ 4 - 28
src/server/ns_turn_server.c

@@ -4400,30 +4400,6 @@ static int refresh_relay_connection(turn_turnserver* server,
 	}
 }
 
-static void write_http_echo(turn_turnserver *server, ts_ur_super_session *ss)
-{
-	if(server && ss && ss->client_socket && !(ss->to_be_closed)) {
-		ioa_network_buffer_handle nbh_http = ioa_network_buffer_allocate(server->e);
-		size_t len_http = ioa_network_buffer_get_size(nbh_http);
-		u08bits *data = ioa_network_buffer_data(nbh_http);
-		char data_http[1025];
-		char content_http[1025];
-		const char* title = "TURN Server";
-		snprintf(content_http,sizeof(content_http)-1,"<!DOCTYPE html>\r\n<html>\r\n  <head>\r\n    <title>%s</title>\r\n  </head>\r\n  <body>\r\n    %s\r\n  </body>\r\n</html>\r\n",title,title);
-		snprintf(data_http,sizeof(data_http)-1,"HTTP/1.1 200 OK\r\nServer: %s\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: %d\r\n\r\n%s",TURN_SOFTWARE,(int)strlen(content_http),content_http);
-		len_http = strlen(data_http);
-		ns_bcopy(data_http,data,len_http);
-		ioa_network_buffer_set_size(nbh_http,len_http);
-		send_data_from_ioa_socket_nbh(ss->client_socket, NULL, nbh_http, TTL_IGNORE, TOS_IGNORE);
-	}
-}
-
-static void handle_https(turn_turnserver *server, ts_ur_super_session *ss, ioa_network_buffer_handle nbh) {
-	//TODO
-	UNUSED_ARG(nbh);
-	write_http_echo(server,ss);
-}
-
 static int read_client_connection(turn_turnserver *server,
 				  	  	  	  	  ts_ur_super_session *ss, ioa_net_data *in_buffer,
 				  	  	  	  	  int can_resume, int count_usage) {
@@ -4466,12 +4442,12 @@ static int read_client_connection(turn_turnserver *server,
 	if(sat == HTTP_CLIENT_SOCKET) {
 
 		TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: HTTP connection input: %s\n", __FUNCTION__, (char*)ioa_network_buffer_data(in_buffer->nbh));
-		write_http_echo(server,ss);
+		write_http_echo(ss);
 
 	} else if(sat == HTTPS_CLIENT_SOCKET) {
 
 		TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: HTTPS connection input: %s\n", __FUNCTION__, (char*)ioa_network_buffer_data(in_buffer->nbh));
-		handle_https(server,ss,in_buffer->nbh);
+		handle_https(ss,in_buffer->nbh);
 
 	} else if (stun_is_channel_message_str(ioa_network_buffer_data(in_buffer->nbh),
 					&blen,
@@ -4566,11 +4542,11 @@ static int read_client_connection(turn_turnserver *server,
 					proto = "HTTPS";
 					set_ioa_socket_app_type(ss->client_socket,HTTPS_CLIENT_SOCKET);
 					TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: %s (%s %s) request: %s\n", __FUNCTION__, proto, get_ioa_socket_cipher(ss->client_socket), get_ioa_socket_ssl_method(ss->client_socket), (char*)ioa_network_buffer_data(in_buffer->nbh));
-					handle_https(server,ss,in_buffer->nbh);
+					handle_https(ss,in_buffer->nbh);
 				} else {
 					set_ioa_socket_app_type(ss->client_socket,HTTP_CLIENT_SOCKET);
 					TURN_LOG_FUNC(TURN_LOG_LEVEL_INFO, "%s: %s request: %s\n", __FUNCTION__, proto, (char*)ioa_network_buffer_data(in_buffer->nbh));
-					write_http_echo(server,ss);
+					write_http_echo(ss);
 				}
 			}
 		}