start of new file
[IRC.git] / Robust / src / Benchmarks / Jhttpp2 / BR / Jhttpp2Task.java
1 task start(StartupObject s{initialstate}) {
2     Jhttpp2Server js=new Jhttpp2Server(true);
3     taskexit(s{!initialstate});
4 }
5
6 task acceptconnection(ServerSocket ss{SocketPending}) {
7     tag t=new tag(connection);
8     MySocket ms=new MySocket(){}{t};
9     ss.accept(ms);
10     Jhttpp2HTTPSession js=new Jhttpp2HTTPSession() {first}{t};
11     Jhttpp2ClientInputStream jcis=new Jhttpp2ClientInputStream() {}{t};
12 }
13
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);
20     }
21     tag ct=new tag(chained);
22     Request r=jcis.readfirst(str, session, ct, tc);
23     if (r==null) {
24         session.request=str;
25         taskexit;
26     } else {
27         session.request=str.substring(r.length(), str.length());
28         if (session.request.length()>0)
29             taskexit(session{more, !first});
30         else
31             taskexit(session{!first});
32     }
33 }
34
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");
40     if (length==0) {
41         socket.close();
42         taskexit;
43     } else if (length < 0 ) {
44         System.printString("ERROR\n");
45         taskexit;
46     }
47     String str=new String(buf, 0, length);
48
49     if (session.request!=null) {
50         str=session.request.concat(str);
51     }
52     //pull off the next request
53     tag ct=new tag(chained);
54     Request r=jcis.read(str, session, ct, tc);
55     if (r==null) {
56         session.request=str;
57         taskexit;
58     } else {
59         session.request=str.substring(r.length(), str.length());
60         if (session.request.length()>0)
61             taskexit(session{more}, rold{chained}{ct});
62         else
63             taskexit(rold{chained}{ct});
64     }
65 }
66
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);
72     if (r==null) {
73         session.request=str;
74         taskexit;
75     } else {
76         session.request=str.substring(r.length(), str.length());
77         if (session.request.length()>0)
78             taskexit(rold{chained}{ct});
79         else
80             taskexit(session{!more}, rold{chained}{ct});
81     }
82 }
83
84 task sendfirst(Request r{first&&!processed}{connection tc}) {
85     r.parseRequest();
86     r.connect();
87     r.sendRequest();
88     taskexit(r{processed});
89 }
90
91 task sendnext(optional Request rprev{received&&!done}{chained ct}, Request r{!processed&&!first}{chained ct}) {
92     r.parseRequest();
93     if (isavailable(rprev)&&rprev.remote_host_name!=null&&
94         rprev.remote_host_name.equals(r.remote_host_name)&&rprev.port==r.port) {
95         r.fd=rprev.fd;
96         r.nativeBindFD(r.fd);
97     } else
98         r.connect();
99     r.sendRequest();
100     taskexit(r{processed}, rprev{done});
101 }
102
103 task recvreq(Request r{processed&&!received&&IOPending}) {
104     byte[] buf=new byte[10000];
105     int length=r.read(buf);
106     if (length==0) {
107         //Done
108         taskexit(r{received});
109     } else if (length<0) {
110         r.close();
111         taskexit(r{received});
112     }
113     String str=new String(buf, 0, length);
114     if (r.response!=null) {
115         str=r.response.concat(str);
116     }
117     boolean cnt=true;
118     int lastindex=0;
119     int bytes=0;
120     while(cnt) {
121         int nextindex=str.indexOf('\n',lastindex)+1;
122         if (nextindex==-1) {
123             r.response=str;
124             taskexit;
125         }
126         if (nextindex-lastindex<=2) {
127             cnt=false;
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"));
132             else 
133                 if(clen.indexOf("\n")!=-1) 
134                     clen=clen.substring(0,clen.indexOf("\n"));
135             bytes=Integer.parseInt(clen);
136         }
137         lastindex=nextindex;
138     }
139     if (bytes>0) {
140         if ((lastindex+bytes)<=str.length()) {
141             r.response=str;
142             taskexit(r{received});
143         } else {
144             r.response=str;
145             taskexit;
146         }
147     } else {
148         //read until 0
149         r.response=str;
150         taskexit;
151     }
152 }
153
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());
157         taskexit(r{sent});
158     } else {
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());
166         taskexit(r{sent});
167     }
168 }
169
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());
173         taskexit(r{sent});
174     } else {
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());
182         taskexit(r{sent});
183     }
184 }