annotate src/goodjava/webserver/Connection.java @ 1503:74c534de211f

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