1 task start(StartupObject s{initialstate}) {
2 Jhttpp2Server js=new Jhttpp2Server(true);
3 taskexit(s{!initialstate});
6 task acceptconnection(ServerSocket ss{SocketPending}) {
7 tag t=new tag(connection);
8 MySocket ms=new MySocket(){}{t};
10 Jhttpp2HTTPSession js=new Jhttpp2HTTPSession() {first}{t};
11 Jhttpp2ClientInputStream jcis=new Jhttpp2ClientInputStream() {}{t};
14 task requestfirst(Jhttpp2HTTPSession session {first}{connection tc}, Jhttpp2ClientInputStream jcis {}{connection tc}, MySocket socket {IOPending}{connection tc}) {
15 byte[] buf=new byte[10000];
16 int length=socket.read(buf);
17 String str=new String(buf, 0, length);
18 if (session.request!=null) {
19 str=session.request.concat(str);
21 tag ct=new tag(chained);
22 Request r=jcis.readfirst(str, session, ct, tc);
27 session.request=str.substring(r.length(), str.length());
28 if (session.request.length()>0)
29 taskexit(session{more, !first});
31 taskexit(session{!first});
35 task request(Jhttpp2HTTPSession session {!first}{connection tc}, Jhttpp2ClientInputStream jcis {}{connection tc}, MySocket socket {IOPending}{connection tc}, optional Request rold{!chained}{connection tc}) {
36 byte[] buf=new byte[10000];
37 int length=socket.read(buf);
38 if (session.request!=null)
39 System.printString(session.request+"\n");
43 } else if (length < 0 ) {
44 System.printString("ERROR\n");
47 String str=new String(buf, 0, length);
49 if (session.request!=null) {
50 str=session.request.concat(str);
52 //pull off the next request
53 tag ct=new tag(chained);
54 Request r=jcis.read(str, session, ct, tc);
59 session.request=str.substring(r.length(), str.length());
60 if (session.request.length()>0)
61 taskexit(session{more}, rold{chained}{ct});
63 taskexit(rold{chained}{ct});
67 task requestmore(Jhttpp2HTTPSession session {more}{connection tc}, Jhttpp2ClientInputStream jcis {}{connection tc}, optional Request rold{!chained}{connection tc}) {
68 String str=session.request;
69 //pull off the next request
70 tag ct=new tag(chained);
71 Request r=jcis.read(str, session, ct, tc);
76 session.request=str.substring(r.length(), str.length());
77 if (session.request.length()>0)
78 taskexit(rold{chained}{ct});
80 taskexit(session{!more}, rold{chained}{ct});
84 task sendfirst(Request r{first&&!processed}{connection tc}) {
88 taskexit(r{processed});
91 task sendnext(optional Request rprev{received&&!done}{chained ct}, Request r{!processed&&!first}{chained ct}) {
93 if (isavailable(rprev)&&rprev.remote_host_name!=null&&
94 rprev.remote_host_name.equals(r.remote_host_name)&&rprev.port==r.port) {
100 taskexit(r{processed}, rprev{done});
103 task recvreq(Request r{processed&&!received&&IOPending}) {
104 byte[] buf=new byte[10000];
105 int length=r.read(buf);
108 taskexit(r{received});
109 } else if (length<0) {
111 taskexit(r{received});
113 String str=new String(buf, 0, length);
114 if (r.response!=null) {
115 str=r.response.concat(str);
121 int nextindex=str.indexOf('\n',lastindex)+1;
126 if (nextindex-lastindex<=2) {
128 } else if (str.substring(lastindex, nextindex+1).toUpperCase().startsWith("CONTENT-LENGTH")) {
129 String clen=str.substring(lastindex+16, nextindex+1);
130 if (clen.indexOf("\r")!=-1)
131 clen=clen.substring(0,clen.indexOf("\r"));
133 if(clen.indexOf("\n")!=-1)
134 clen=clen.substring(0,clen.indexOf("\n"));
135 bytes=Integer.parseInt(clen);
140 if ((lastindex+bytes)<=str.length()) {
142 taskexit(r{received});
154 task sendfirstresp(optional Request r{first && !sent && received}{connection tc}, MySocket sock{}{connection tc}) {
155 if (isavailable(r)&&r.response!=null) {
156 sock.write(r.response.getBytes());
159 String msg="<HTML><HEAD><TITLE> 503 Service Unavailable </TITLE></HEAD>\r<BODY>Request Failed</BODY></HTML>\r\n";
160 String resp="HTTP/1.1 503 Service Unavailable\r\n"+
161 "Cache-Control: nocache, must-revalidate\r\n"+
162 "Connection: close\r\n"+
163 "Content-Length: "+msg.length()+"\r\n"+
164 "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"+msg;
165 sock.write(resp.getBytes());
170 task sendresp(optional Request rprev {sent}{chained ct}, optional Request r{!sent && received && !first}{chained ct, connection tc}, MySocket sock{}{connection tc}) {
171 if (isavailable(r)&&r.response!=null) {
172 sock.write(r.response.getBytes());
175 String msg="<HTML><HEAD><TITLE> 503 Service Unavailable </TITLE></HEAD>\r<BODY>Request Failed</BODY></HTML>\r\n";
176 String resp="HTTP/1.1 503 Service Unavailable\r\n"+
177 "Cache-Control: nocache, must-revalidate\r\n"+
178 "Connection: close\r\n"+
179 "Content-Length: "+msg.length()+"\r\n"+
180 "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"+msg;
181 sock.write(resp.getBytes());