changeset 165:47ab97730865

Fix a couple of issues in networking lib
author Mike Pavone <pavone@retrodev.com>
date Thu, 10 Mar 2011 04:15:37 +0000
parents f5095855c878
children 1bfc19076f1b
files framework.rhope net.rhope parse.rhope runtime/net.c webserver.rhope
diffstat 5 files changed, 67 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/framework.rhope	Fri Jan 07 03:19:26 2011 -0500
+++ b/framework.rhope	Thu Mar 10 04:15:37 2011 +0000
@@ -27,7 +27,12 @@
 Framework Handler[con,path,request type,queryvars,headers,handler,title,use session]
 {
 	page <- Page[title, path, use session, queryvars, headers]
-	handler page <- [handler]Call[page,path]
+	hstart <- time[0i64]
+	{ handler page <- [handler]Call[page,path]
+	{
+		hend <- time[0i64]
+		Print[["Handler took: "]Append[String[[hend]-[hstart]]]]
+	}}
 	If[[request type] = ["POST"]]
 	{
 		final page,ncon <- Process POST[handler page, con, headers]
@@ -35,7 +40,13 @@
 		final page <- Val[handler page]
 		ncon <- Val[con]
 	}
-	string,out headers <- [final page]Render
+	Val[final page]
+	{ rstart <- time[0i64]
+	{ string,out headers <- [final page]Render
+	{
+		rend <- time[0i64]
+		Print[["Render took: "]Append[String[[rend]-[rstart]]]]
+	}}}
 	
 	[[string]Write to File[HTTP OK[ncon, Get Content Type[".html"], [string]Byte Length, out headers]]]Close
 }
@@ -458,17 +469,16 @@
 
 Escape HTML Text[string:out]
 {
-	out <- [[[string]Replace["&","&amp;"]]Replace["<", "&lt;"]]Replace[">", "&gt;"]
+	out <- [string]Replace[("&","<",">"),("&amp;","&lt;","&gt;")]
 }
 	
 Render@Web Text[text:out,headers]
 {
-	escaped <- Escape HTML Text[[text]Text >>]
 	If[[text]Preformatted >>]
 	{
-		processed text <- Val[escaped]
+		processed text <- Escape HTML Text[[text]Text >>]
 	}{
-		processed text <- [escaped]Replace["\n","<br>\n\t"]
+		processed text <- [[text]Text >>]Replace[("&","<",">","\n"),("&amp;","&lt;","&gt;","<br>\n\t")]
 	}
 	If[[[[text]Enclosing Tag >>]Length] = [0]]
 	{
--- a/net.rhope	Fri Jan 07 03:19:26 2011 -0500
+++ b/net.rhope	Thu Mar 10 04:15:37 2011 +0000
@@ -19,6 +19,7 @@
 	_internal_bindnewsocket[port(Int32,Naked),setreuse(Int32,Naked):socket(Int32,Naked)]
 	_internal_accept[sockfd(Int32,Naked),addrbuf(Array,Raw Pointer,Mutable),buflen(Int32,Naked):consock(Int32,Naked),addrbuf]
 	_internal_connectnewsocket[addr(Array,Raw Pointer),port(Int32,Naked):sockfd(Int32,Naked)]
+	_internal_ignoresigpipe[:out(Int32,Naked)]
 }
 
 Blueprint epoll_event
@@ -69,8 +70,9 @@
 				{ res <- No }
 				{ res <- Yes }
 				ct,cont <- _Get IO Context[activefd]
-				{
+				{ 
 				}{ Print["Could not find context for IO event"] }
+
 				,cont <- Resume[ct,res]
 				{
 					cont <- Yield[]
@@ -112,6 +114,7 @@
 
 _Sock Listener[pipefd]
 {
+	_internal_ignoresigpipe[]
 	epfd <- epoll_create[16]
 	If[[epfd]=[-1]]
 	{
@@ -133,7 +136,7 @@
 	pipefd <- -1
 }
 
-_Add FD to Listener[fd,context:pipefd,err] uses socklisten
+__Add FD to Listener[fd,context:pipefd,err,startpipe] uses socklisten
 {
 	If[socklisten::listener started]
 	{
@@ -154,7 +157,7 @@
 			fcntl[[des]Index[0], 4i32, 2048i64]
 			fcntl[[des]Index[1], 4i32, 2048i64]
 			socklisten::listener started <- Yes
-			Call Async[_Sock Listener[[des]Index[0],?]]
+			startpipe <- [des]Index[0]
 			do add <- Yes
 		}
 	}
@@ -164,6 +167,12 @@
 	}
 }
 
+_Add FD to Listener[fd,context:pipefd,err]
+{
+	pipefd,err <- __Add FD to Listener[fd,context] {} {}
+	{ Call Async[_Sock Listener[~,?]] }
+}
+
 _Write to Listener Pipe[pipefd,data]
 {
 	res <- write[pipefd, data, 8i64]
@@ -270,14 +279,18 @@
 
 _Write@TCP Connection[con,buffer,wrote:out,err]
 {
-	
 	new wrote <- write[[con]Filedes >>, buffer, Int64[[buffer]Length]]
-	If[[new wrote]=[-1]]
+	If[[new wrote]<[1]]
 	{
-		If[Wait for IO[[con]Filedes >>, 4]]
+		,doerr <- If[new wrote]
 		{
-			out,err <- [con]_Write[buffer,wrote]
-		}{
+			,doerr <- If[Wait for IO[[con]Filedes >>, 4]]
+			{
+				out,err <- [con]_Write[buffer,wrote]
+			}
+		}
+		Val[doerr]
+		{
 			err <- wrote
 		}
 	}{
@@ -439,12 +452,17 @@
 	{ numread <- Trunc Int32[~] }
 	{ outbuf <- [~]Length <<[numread] }
 	
-	If[[numread]=[-1]]
+	If[[numread]<[1]]
 	{
-		If[Wait for IO[[con]Filedes >>, 1]]
+		doerr <- If[[numread]=[0]] {}
 		{
-			data,out con,err <- [con]_Read Delim[delim,poffset,buflist]
-		}{
+			,doerr <- If[Wait for IO[[con]Filedes >>, 1]]
+			{
+				data,out con,err <- [con]_Read Delim[delim,poffset,buflist]
+			}
+		}
+		Val[doerr]
+		{
 			err <- [Fold[_Add Len[?], 0, buflist]]+[Length[[con]Read Buffer >>]]
 		}
 	}{
@@ -544,7 +562,7 @@
 		poffset <- -1
 		
 	}
-	data,out con,err <- [ncon]_Read Delim[delim,poffset,buflist]
+	data,out con,err <- [ncon]_Read Delim[delim,poffset,buflist] {} {}
 }
 
 Close@TCP Connection[con:out]
--- a/parse.rhope	Fri Jan 07 03:19:26 2011 -0500
+++ b/parse.rhope	Thu Mar 10 04:15:37 2011 +0000
@@ -619,9 +619,10 @@
 
 Parse[tokens:out]
 {
-	[tokens]First
+	ftokens <- Filter[tokens, NotComment[?]]
+	[ftokens]First
 	{
-		out <- Top Level[Filter[tokens, NotComment[?]], ~, ()]
+		out <- Top Level[ftokens, ~, ()]
 	}{
 		out <- ()
 	}
--- a/runtime/net.c	Fri Jan 07 03:19:26 2011 -0500
+++ b/runtime/net.c	Thu Mar 10 04:15:37 2011 +0000
@@ -4,6 +4,7 @@
 #include <netinet/in.h>
 #include <string.h>
 #include <stdio.h>
+#include <signal.h>
 
 int _internal_accept(int sockfd, char * addrbuf, int buflen)
 {
@@ -88,3 +89,11 @@
 	return sock;
 }
 
+int _internal_ignoresigpipe()
+{
+	struct sigaction act;
+	memset(&act, 0, sizeof(act));
+	act.sa_handler = SIG_IGN;
+	return sigaction(SIGPIPE, &act, NULL);
+}
+
--- a/webserver.rhope	Fri Jan 07 03:19:26 2011 -0500
+++ b/webserver.rhope	Thu Mar 10 04:15:37 2011 +0000
@@ -164,8 +164,13 @@
 
 Connection Start[con,address,handlers]
 {
+	Print["Connection Start"]
 	,client <- [con]Read Delim[["\r\n"]Buffer >>]
-	{ request <- String[~] }
+	{ request <- String[~] } {}
+	{ 
+		Print["Error reading request line"] 
+		Close[con]
+	}
 	parts <- [request]Split[" "]
 	Print[[[request]Append[" "]]Append[address]]
 	[client]Read Delim[["\r\n\r\n"]Buffer >>]
@@ -173,6 +178,8 @@
 		headers <- Map[Dict Split[String[~], ":", "\r\n"], Trim[?, " \t"]]
 	}{
 		Handle Request[~, [parts]Index[0], [parts]Index[1], headers, handlers]
+	}{
+		Print["Error reading headers"]
 	}
 }