comparison src/luan/webserver/Connection.java @ 1194:bd0420fb3dd0

handle ParseException in webserver
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 26 Feb 2018 16:29:07 -0700
parents e2d2354807f3
children 44491798e431
comparison
equal deleted inserted replaced
1193:9f522cfe0c30 1194:bd0420fb3dd0
25 } 25 }
26 26
27 private void handle() { 27 private void handle() {
28 try { 28 try {
29 Request request = new Request(); 29 Request request = new Request();
30 { 30 Response response;
31 InputStream in = socket.getInputStream(); 31 try {
32 byte[] a = new byte[8192]; 32 {
33 int endOfHeader; 33 InputStream in = socket.getInputStream();
34 int size = 0; 34 byte[] a = new byte[8192];
35 int left = a.length; 35 int endOfHeader;
36 outer: while(true) { 36 int size = 0;
37 int n = in.read(a,size,left); 37 int left = a.length;
38 if( n == -1 ) { 38 outer: while(true) {
39 if( size == 0 ) { 39 int n = in.read(a,size,left);
40 socket.close();
41 return;
42 }
43 throw new IOException("unexpected end of input at "+size);
44 }
45 size += n;
46 for( int i=0; i<=size-4; i++ ) {
47 if( a[i]=='\r' && a[i+1]=='\n' && a[i+2]=='\r' && a[i+3]=='\n' ) {
48 endOfHeader = i + 4;
49 break outer;
50 }
51 }
52 left -= n;
53 if( left == 0 ) {
54 byte[] a2 = new byte[2*a.length];
55 System.arraycopy(a,0,a2,0,size);
56 a = a2;
57 left = a.length - size;
58 }
59 }
60 String rawHead = new String(a,0,endOfHeader);
61 //System.out.println(rawHead);
62 request.rawHead = rawHead;
63 RequestParser parser = new RequestParser(request);
64 parser.parseHead();
65
66 String lenStr = (String)request.headers.get("content-length");
67 if( lenStr != null ) {
68 int len = Integer.parseInt(lenStr);
69 byte[] body = new byte[len];
70 size -= endOfHeader;
71 System.arraycopy(a,endOfHeader,body,0,size);
72 while( size < len ) {
73 int n = in.read(body,size,len-size);
74 if( n == -1 ) { 40 if( n == -1 ) {
41 if( size == 0 ) {
42 socket.close();
43 return;
44 }
75 throw new IOException("unexpected end of input at "+size); 45 throw new IOException("unexpected end of input at "+size);
76 } 46 }
77 size += n; 47 size += n;
48 for( int i=0; i<=size-4; i++ ) {
49 if( a[i]=='\r' && a[i+1]=='\n' && a[i+2]=='\r' && a[i+3]=='\n' ) {
50 endOfHeader = i + 4;
51 break outer;
52 }
53 }
54 left -= n;
55 if( left == 0 ) {
56 byte[] a2 = new byte[2*a.length];
57 System.arraycopy(a,0,a2,0,size);
58 a = a2;
59 left = a.length - size;
60 }
78 } 61 }
79 request.body = body; 62 String rawHead = new String(a,0,endOfHeader);
63 //System.out.println(rawHead);
64 request.rawHead = rawHead;
65 RequestParser parser = new RequestParser(request);
66 parser.parseHead();
67
68 String lenStr = (String)request.headers.get("content-length");
69 if( lenStr != null ) {
70 int len = Integer.parseInt(lenStr);
71 byte[] body = new byte[len];
72 size -= endOfHeader;
73 System.arraycopy(a,endOfHeader,body,0,size);
74 while( size < len ) {
75 int n = in.read(body,size,len-size);
76 if( n == -1 ) {
77 throw new IOException("unexpected end of input at "+size);
78 }
79 size += n;
80 }
81 request.body = body;
80 //System.out.println(new String(request.body)); 82 //System.out.println(new String(request.body));
81 } 83 }
82 84
83 String contentType = (String)request.headers.get("content-type"); 85 String contentType = (String)request.headers.get("content-type");
84 if( contentType != null ) { 86 if( contentType != null ) {
85 if( request.body == null ) { 87 if( request.body == null ) {
86 logger.error("body is null"); 88 logger.error("body is null");
87 } else {
88 if( "application/x-www-form-urlencoded".equals(contentType) ) {
89 parser.parseUrlencoded();
90 } else if( contentType.startsWith("multipart/form-data;") ) {
91 parser.parseMultipart();
92 } else { 89 } else {
93 logger.error("unknown content type: "+contentType); 90 if( "application/x-www-form-urlencoded".equals(contentType) ) {
91 parser.parseUrlencoded();
92 } else if( contentType.startsWith("multipart/form-data;") ) {
93 parser.parseMultipart();
94 } else {
95 logger.error("unknown content type: "+contentType);
96 }
94 } 97 }
95 } 98 }
96 } 99 }
100 response = server.handler.handle(request);
101 } catch(ParseException e) {
102 logger.warn("",e);
103 response = Response.errorResponse(Status.BAD_REQUEST,e.toString());
97 } 104 }
98
99 Response response = server.handler.handle(request);
100 response.headers.put("connection","close"); 105 response.headers.put("connection","close");
101 response.headers.put("content-length",Long.toString(response.body.length)); 106 response.headers.put("content-length",Long.toString(response.body.length));
102 byte[] header = response.toHeaderString().getBytes(); 107 byte[] header = response.toHeaderString().getBytes();
103 108
104 OutputStream out = socket.getOutputStream(); 109 OutputStream out = socket.getOutputStream();
106 copyAll(response.body.content,out); 111 copyAll(response.body.content,out);
107 out.close(); 112 out.close();
108 socket.close(); 113 socket.close();
109 } catch(IOException e) { 114 } catch(IOException e) {
110 logger.info("",e); 115 logger.info("",e);
111 } catch(ParseException e) {
112 logger.warn("",e);
113 } 116 }
114 } 117 }
115 118
116 private static void copyAll(InputStream in,OutputStream out) 119 private static void copyAll(InputStream in,OutputStream out)
117 throws IOException 120 throws IOException