Browse Source

better http/1.1 support

mom040267 11 years ago
parent
commit
0551cfcc9c
3 changed files with 52 additions and 8 deletions
  1. 28 1
      src/apps/relay/http_server.c
  2. 3 0
      src/apps/relay/http_server.h
  3. 21 7
      src/apps/relay/turn_admin_server.c

+ 28 - 1
src/apps/relay/http_server.c

@@ -35,6 +35,8 @@
 #include <event2/http.h>
 #include <event2/keyvalq_struct.h>
 
+#include <time.h>
+
 //////////////////////////////////////
 
 struct headers_list {
@@ -62,7 +64,7 @@ static void write_http_echo(ioa_socket_handle s)
 			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    <b>%s</b> <br> <b><i>use https connection for the admin session</i></b>\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);
+			snprintf(data_http,sizeof(data_http)-1,"HTTP/1.0 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);
@@ -75,6 +77,28 @@ void handle_http_echo(ioa_socket_handle s) {
 	write_http_echo(s);
 }
 
+const char* get_http_date_header()
+{
+	static char buffer_date[256];
+	static char buffer_header[1025];
+	static const char* wds[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
+	static const char* mons[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
+
+	time_t now = time(NULL);
+	struct tm *gmtm = gmtime(&now);
+
+	buffer_header[0]=0;
+	buffer_date[0]=0;
+	if(gmtm) {
+		snprintf(buffer_date,sizeof(buffer_date)-1,"%s, %d %s %d %d:%d:%d GMT",wds[gmtm->tm_wday], gmtm->tm_mday, mons[gmtm->tm_mon], gmtm->tm_year+1900, gmtm->tm_hour, gmtm->tm_min, gmtm->tm_sec);
+		buffer_date[sizeof(buffer_date)-1]=0;
+		snprintf(buffer_header,sizeof(buffer_header)-1,"Date: %s\r\n",buffer_date);
+		buffer_header[sizeof(buffer_header)-1]=0;
+	}
+
+	return buffer_header;
+}
+
 ///////////////////////////////////////////////
 
 static struct headers_list * post_parse(char *data, size_t data_len)
@@ -177,6 +201,9 @@ struct http_request* parse_http_request(char* request) {
 		if(strstr(request,"GET ") == request) {
 			ret->rtype = HRT_GET;
 			ret = parse_http_request_1(ret,request+4,0);
+		} else if(strstr(request,"HEAD ") == request) {
+			ret->rtype = HRT_HEAD;
+			ret = parse_http_request_1(ret,request+5,0);
 		} else if(strstr(request,"POST ") == request) {
 			ret->rtype = HRT_POST;
 			ret = parse_http_request_1(ret,request+5,1);

+ 3 - 0
src/apps/relay/http_server.h

@@ -47,6 +47,7 @@ extern "C" {
 enum _HTTP_REQUEST_TYPE {
 	HRT_UNKNOWN=0,
 	HRT_GET,
+	HRT_HEAD,
 	HRT_POST,
 	HRT_PUT,
 	HRT_DELETE
@@ -66,6 +67,8 @@ struct http_request* parse_http_request(char* request);
 const char *get_http_header_value(const struct http_request *request, const char* key, const char* def);
 void free_http_request(struct http_request *request);
 
+const char* get_http_date_header(void);
+
 ////////////////////////////////////////////
 
 struct str_buffer;

+ 21 - 7
src/apps/relay/turn_admin_server.c

@@ -1485,7 +1485,9 @@ static void write_https_logon_page(ioa_socket_handle s)
 
 		str_buffer_append(sb_http,"HTTP/1.1 200 OK\r\nServer: ");
 		str_buffer_append(sb_http,TURN_SOFTWARE);
-		str_buffer_append(sb_http,"\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: ");
+		str_buffer_append(sb_http,"\r\n");
+		str_buffer_append(sb_http,get_http_date_header());
+		str_buffer_append(sb_http,"Content-Type: text/html; charset=UTF-8\r\nContent-Length: ");
 		str_buffer_append_sz(sb_http,str_buffer_get_str_len(sb));
 		str_buffer_append(sb_http,"\r\n\r\n");
 		str_buffer_append(sb_http,str_buffer_get_str(sb));
@@ -1561,7 +1563,9 @@ static void write_https_home_page(ioa_socket_handle s)
 
 			send_str_from_ioa_socket_tcp(s,"HTTP/1.1 200 OK\r\nServer: ");
 			send_str_from_ioa_socket_tcp(s,TURN_SOFTWARE);
-			send_str_from_ioa_socket_tcp(s,"\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: ");
+			send_str_from_ioa_socket_tcp(s,"\r\n");
+			send_str_from_ioa_socket_tcp(s,get_http_date_header());
+			send_str_from_ioa_socket_tcp(s,"Content-Type: text/html; charset=UTF-8\r\nContent-Length: ");
 
 			send_ulong_from_ioa_socket_tcp(s,str_buffer_get_str_len(sb));
 
@@ -2026,7 +2030,9 @@ static void write_pc_page(ioa_socket_handle s)
 
 			send_str_from_ioa_socket_tcp(s,"HTTP/1.1 200 OK\r\nServer: ");
 			send_str_from_ioa_socket_tcp(s,TURN_SOFTWARE);
-			send_str_from_ioa_socket_tcp(s,"\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: ");
+			send_str_from_ioa_socket_tcp(s,"\r\n");
+			send_str_from_ioa_socket_tcp(s,get_http_date_header());
+			send_str_from_ioa_socket_tcp(s,"Content-Type: text/html; charset=UTF-8\r\nContent-Length: ");
 
 			send_ulong_from_ioa_socket_tcp(s,str_buffer_get_str_len(sb));
 
@@ -2275,7 +2281,9 @@ static void write_ps_page(ioa_socket_handle s, const char* client_protocol, cons
 
 			send_str_from_ioa_socket_tcp(s,"HTTP/1.1 200 OK\r\nServer: ");
 			send_str_from_ioa_socket_tcp(s,TURN_SOFTWARE);
-			send_str_from_ioa_socket_tcp(s,"\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: ");
+			send_str_from_ioa_socket_tcp(s,"\r\n");
+			send_str_from_ioa_socket_tcp(s,get_http_date_header());
+			send_str_from_ioa_socket_tcp(s,"Content-Type: text/html; charset=UTF-8\r\nContent-Length: ");
 
 			send_ulong_from_ioa_socket_tcp(s,str_buffer_get_str_len(sb));
 
@@ -2440,7 +2448,9 @@ static void write_users_page(ioa_socket_handle s, const u08bits *add_user, const
 
 			send_str_from_ioa_socket_tcp(s,"HTTP/1.1 200 OK\r\nServer: ");
 			send_str_from_ioa_socket_tcp(s,TURN_SOFTWARE);
-			send_str_from_ioa_socket_tcp(s,"\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: ");
+			send_str_from_ioa_socket_tcp(s,"\r\n");
+			send_str_from_ioa_socket_tcp(s,get_http_date_header());
+			send_str_from_ioa_socket_tcp(s,"Content-Type: text/html; charset=UTF-8\r\nContent-Length: ");
 
 			send_ulong_from_ioa_socket_tcp(s,str_buffer_get_str_len(sb));
 
@@ -2591,7 +2601,9 @@ static void write_shared_secrets_page(ioa_socket_handle s, const char* add_secre
 
 			send_str_from_ioa_socket_tcp(s,"HTTP/1.1 200 OK\r\nServer: ");
 			send_str_from_ioa_socket_tcp(s,TURN_SOFTWARE);
-			send_str_from_ioa_socket_tcp(s,"\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: ");
+			send_str_from_ioa_socket_tcp(s,"\r\n");
+			send_str_from_ioa_socket_tcp(s,get_http_date_header());
+			send_str_from_ioa_socket_tcp(s,"Content-Type: text/html; charset=UTF-8\r\nContent-Length: ");
 
 			send_ulong_from_ioa_socket_tcp(s,str_buffer_get_str_len(sb));
 
@@ -2741,7 +2753,9 @@ static void write_origins_page(ioa_socket_handle s, const char* add_origin, cons
 
 			send_str_from_ioa_socket_tcp(s,"HTTP/1.1 200 OK\r\nServer: ");
 			send_str_from_ioa_socket_tcp(s,TURN_SOFTWARE);
-			send_str_from_ioa_socket_tcp(s,"\r\nContent-Type: text/html; charset=UTF-8\r\nContent-Length: ");
+			send_str_from_ioa_socket_tcp(s,"\r\n");
+			send_str_from_ioa_socket_tcp(s,get_http_date_header());
+			send_str_from_ioa_socket_tcp(s,"Content-Type: text/html; charset=UTF-8\r\nContent-Length: ");
 
 			send_ulong_from_ioa_socket_tcp(s,str_buffer_get_str_len(sb));