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