comparison src/luan/webserver/Connection.java @ 1144:ae0a048f3bc7

webserver - handle POST params
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 31 Jan 2018 00:29:50 -0700
parents 0f59eab45f3d
children 12ececf30597
comparison
equal deleted inserted replaced
1143:3bf5190b3c77 1144:ae0a048f3bc7
24 this.socket = socket; 24 this.socket = socket;
25 } 25 }
26 26
27 private void handle() { 27 private void handle() {
28 try { 28 try {
29 InputStream in = socket.getInputStream(); 29 Request request = new Request();
30 byte[] a = new byte[8192]; 30 {
31 int endOfHeader; 31 InputStream in = socket.getInputStream();
32 int size = 0; 32 byte[] a = new byte[8192];
33 int left = a.length; 33 int endOfHeader;
34 outer: while(true) { 34 int size = 0;
35 int n = in.read(a,size,left); 35 int left = a.length;
36 if( n == -1 ) { 36 outer: while(true) {
37 if( size == 0 ) { 37 int n = in.read(a,size,left);
38 socket.close(); 38 if( n == -1 ) {
39 return; 39 if( size == 0 ) {
40 socket.close();
41 return;
42 }
43 throw new IOException("unexpected end of input at "+size);
40 } 44 }
41 throw new IOException("unexpected end of input at "+size); 45 size += n;
42 } 46 for( int i=0; i<=size-4; i++ ) {
43 size += n; 47 if( a[i]=='\r' && a[i+1]=='\n' && a[i+2]=='\r' && a[i+3]=='\n' ) {
44 for( int i=0; i<=size-4; i++ ) { 48 endOfHeader = i + 4;
45 if( a[i]=='\r' && a[i+1]=='\n' && a[i+2]=='\r' && a[i+3]=='\n' ) { 49 break outer;
46 endOfHeader = i + 4; 50 }
47 break outer; 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;
48 } 58 }
49 } 59 }
50 left -= n; 60 String rawHead = new String(a,0,endOfHeader);
51 if( left == 0 ) { 61 //System.out.println(rawHead);
52 byte[] a2 = new byte[2*a.length]; 62 request.rawHead = rawHead;
53 System.arraycopy(a,0,a2,0,size); 63 RequestParser parser = new RequestParser(request);
54 a = a2; 64 parser.parseHead();
55 left = a.length - size; 65
66 String lenStr = 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 ) {
75 throw new IOException("unexpected end of input at "+size);
76 }
77 size += n;
78 }
79 request.body = new String(body);
80 //System.out.println(request.body);
81 }
82
83 if( request.method.equals("POST") ) {
84 if( request.body == null ) {
85 logger.error("post body is null");
86 } else {
87 parser.parsePost();
88 }
56 } 89 }
57 } 90 }
58 String rawRequest = new String(a,0,endOfHeader);
59 //System.out.println(rawRequest);
60 Request request = RequestHeadParser.parse(rawRequest);
61 //System.out.println(request.headers);
62 91
63 Response response = server.handler.handle(request); 92 Response response = server.handler.handle(request);
64 response.headers.put("Connection","close"); 93 response.headers.put("Connection","close");
65 response.headers.put("Content-Length",Long.toString(response.body.length)); 94 response.headers.put("Content-Length",Long.toString(response.body.length));
66 byte[] header = response.toHeaderString().getBytes(); 95 byte[] header = response.toHeaderString().getBytes();
71 out.close(); 100 out.close();
72 socket.close(); 101 socket.close();
73 } catch(IOException e) { 102 } catch(IOException e) {
74 logger.info("",e); 103 logger.info("",e);
75 } catch(ParseException e) { 104 } catch(ParseException e) {
76 logger.info("",e); 105 logger.warn("",e);
77 } 106 }
78 } 107 }
79 108
80 private static void copyAll(InputStream in,OutputStream out) 109 private static void copyAll(InputStream in,OutputStream out)
81 throws IOException 110 throws IOException