Mercurial Hosting > luan
annotate src/goodjava/webserver/Connection.java @ 1736:a02a75e3daa8
webserver error handling
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 16 Oct 2022 11:44:23 -0600 |
parents | fa066aaa068c |
children | 9713f7fd50b3 |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
1 package goodjava.webserver; |
1137 | 2 |
3 import java.io.InputStream; | |
4 import java.io.OutputStream; | |
5 import java.io.IOException; | |
6 import java.net.Socket; | |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
7 import goodjava.logging.Logger; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
8 import goodjava.logging.LoggerFactory; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
9 import goodjava.parser.ParseException; |
1493 | 10 import goodjava.io.IoUtils; |
1137 | 11 |
12 | |
13 final class Connection { | |
14 private static final Logger logger = LoggerFactory.getLogger(Connection.class); | |
15 | |
1142
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
16 static void handle(Server server,Socket socket) { |
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
17 new Connection(server,socket).handle(); |
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
18 } |
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
19 |
1137 | 20 private final Server server; |
21 private final Socket socket; | |
22 | |
1142
0f59eab45f3d
webserver - threading
Franklin Schmidt <fschmidt@gmail.com>
parents:
1138
diff
changeset
|
23 private Connection(Server server,Socket socket) { |
1137 | 24 this.server = server; |
25 this.socket = socket; | |
26 } | |
27 | |
28 private void handle() { | |
1736
a02a75e3daa8
webserver error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1607
diff
changeset
|
29 String rawHead = ""; |
1137 | 30 try { |
1144
ae0a048f3bc7
webserver - handle POST params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1142
diff
changeset
|
31 Request request = new Request(); |
1194
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
32 Response response; |
1513 | 33 String contentType = null; |
1194
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
34 try { |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
35 { |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
36 InputStream in = socket.getInputStream(); |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
37 byte[] a = new byte[8192]; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
38 int endOfHeader; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
39 int size = 0; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
40 int left = a.length; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
41 outer: while(true) { |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
42 int n = in.read(a,size,left); |
1144
ae0a048f3bc7
webserver - handle POST params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1142
diff
changeset
|
43 if( n == -1 ) { |
1194
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
44 if( size == 0 ) { |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
45 socket.close(); |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
46 return; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
47 } |
1144
ae0a048f3bc7
webserver - handle POST params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1142
diff
changeset
|
48 throw new IOException("unexpected end of input at "+size); |
ae0a048f3bc7
webserver - handle POST params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1142
diff
changeset
|
49 } |
ae0a048f3bc7
webserver - handle POST params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1142
diff
changeset
|
50 size += n; |
1194
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
51 for( int i=0; i<=size-4; i++ ) { |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
52 if( a[i]=='\r' && a[i+1]=='\n' && a[i+2]=='\r' && a[i+3]=='\n' ) { |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
53 endOfHeader = i + 4; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
54 break outer; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
55 } |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
56 } |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
57 left -= n; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
58 if( left == 0 ) { |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
59 byte[] a2 = new byte[2*a.length]; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
60 System.arraycopy(a,0,a2,0,size); |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
61 a = a2; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
62 left = a.length - size; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
63 } |
1144
ae0a048f3bc7
webserver - handle POST params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1142
diff
changeset
|
64 } |
1736
a02a75e3daa8
webserver error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1607
diff
changeset
|
65 rawHead = new String(a,0,endOfHeader); |
1197
886e14903c1e
better Content-Type handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1196
diff
changeset
|
66 //System.out.println(rawHead); |
1194
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
67 request.rawHead = rawHead; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
68 RequestParser parser = new RequestParser(request); |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
69 parser.parseHead(); |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
70 |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
71 String lenStr = (String)request.headers.get("content-length"); |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
72 if( lenStr != null ) { |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
73 int len = Integer.parseInt(lenStr); |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
74 byte[] body = new byte[len]; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
75 size -= endOfHeader; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
76 System.arraycopy(a,endOfHeader,body,0,size); |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
77 while( size < len ) { |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
78 int n = in.read(body,size,len-size); |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
79 if( n == -1 ) { |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
80 throw new IOException("unexpected end of input at "+size); |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
81 } |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
82 size += n; |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
83 } |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
84 request.body = body; |
1197
886e14903c1e
better Content-Type handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1196
diff
changeset
|
85 //System.out.println(new String(request.body)); |
1194
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
86 } |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
87 |
1513 | 88 contentType = (String)request.headers.get("content-type"); |
1194
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
89 if( contentType != null ) { |
1266
05934fbf635a
content-type "application/x-www-form-urlencoded; charset=utf-8"
Franklin Schmidt <fschmidt@gmail.com>
parents:
1258
diff
changeset
|
90 contentType = contentType.toLowerCase(); |
1463
fb003c4003dd
better application/json handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
91 if( contentType.equals("application/x-www-form-urlencoded") ) { |
1266
05934fbf635a
content-type "application/x-www-form-urlencoded; charset=utf-8"
Franklin Schmidt <fschmidt@gmail.com>
parents:
1258
diff
changeset
|
92 parser.parseUrlencoded(null); |
1463
fb003c4003dd
better application/json handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
93 } else if( contentType.equals("application/x-www-form-urlencoded; charset=utf-8") ) { |
1266
05934fbf635a
content-type "application/x-www-form-urlencoded; charset=utf-8"
Franklin Schmidt <fschmidt@gmail.com>
parents:
1258
diff
changeset
|
94 parser.parseUrlencoded("utf-8"); |
1197
886e14903c1e
better Content-Type handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1196
diff
changeset
|
95 } else if( contentType.startsWith("multipart/form-data;") ) { |
886e14903c1e
better Content-Type handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1196
diff
changeset
|
96 parser.parseMultipart(); |
1463
fb003c4003dd
better application/json handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
97 } else if( contentType.equals("application/json") ) { |
fb003c4003dd
better application/json handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
98 parser.parseJson(null); |
1258
e4d7a3114fa8
support "Content-Type: application/json; charset=utf-8"
Franklin Schmidt <fschmidt@gmail.com>
parents:
1237
diff
changeset
|
99 } else if( contentType.equals("application/json; charset=utf-8") ) { |
1463
fb003c4003dd
better application/json handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1402
diff
changeset
|
100 parser.parseJson("utf-8"); |
1145 | 101 } else { |
1229 | 102 logger.info("unknown request content-type: "+contentType); |
1145 | 103 } |
1144
ae0a048f3bc7
webserver - handle POST params
Franklin Schmidt <fschmidt@gmail.com>
parents:
1142
diff
changeset
|
104 } |
1237 | 105 |
106 String scheme = (String)request.headers.get("x-forwarded-proto"); | |
107 if( scheme != null ) | |
108 request.scheme = scheme; | |
1137 | 109 } |
1194
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
110 response = server.handler.handle(request); |
bd0420fb3dd0
handle ParseException in webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
1162
diff
changeset
|
111 } catch(ParseException e) { |
1229 | 112 logger.warn("parse error\n"+request.rawHead.trim()+"\n",e); |
1513 | 113 String msg = e.toString(); |
114 if( contentType != null ) | |
115 msg = "invalid content for content-type " + contentType + "\n" + msg; | |
116 response = Response.errorResponse(Status.BAD_REQUEST,msg); | |
1137 | 117 } |
1607 | 118 response.headers.put("Connection","close"); |
119 response.headers.put("Content-Length",Long.toString(response.body.length)); | |
1137 | 120 byte[] header = response.toHeaderString().getBytes(); |
121 | |
122 OutputStream out = socket.getOutputStream(); | |
123 out.write(header); | |
1493 | 124 IoUtils.copyAll(response.body.content,out); |
1137 | 125 out.close(); |
126 socket.close(); | |
127 } catch(IOException e) { | |
1736
a02a75e3daa8
webserver error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1607
diff
changeset
|
128 logger.info(rawHead.trim()+"\n",e); |
a02a75e3daa8
webserver error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1607
diff
changeset
|
129 } catch(RuntimeException e) { |
a02a75e3daa8
webserver error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1607
diff
changeset
|
130 logger.error(rawHead.trim()+"\n",e); |
a02a75e3daa8
webserver error handling
Franklin Schmidt <fschmidt@gmail.com>
parents:
1607
diff
changeset
|
131 throw e; |
1137 | 132 } |
133 } | |
134 | |
135 } |