Mercurial Hosting > luan
diff src/goodjava/webserver/Connection.java @ 1738:9713f7fd50b3
server-sent events
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 03 Nov 2022 19:23:53 -0600 |
parents | a02a75e3daa8 |
children | c7b3c327248a |
line wrap: on
line diff
--- a/src/goodjava/webserver/Connection.java Tue Oct 18 22:08:29 2022 -0600 +++ b/src/goodjava/webserver/Connection.java Thu Nov 03 19:23:53 2022 -0600 @@ -32,81 +32,85 @@ Response response; String contentType = null; try { - { - InputStream in = socket.getInputStream(); - byte[] a = new byte[8192]; - int endOfHeader; - int size = 0; - int left = a.length; - outer: while(true) { - int n = in.read(a,size,left); + InputStream in = socket.getInputStream(); + byte[] a = new byte[8192]; + int endOfHeader; + int size = 0; + int left = a.length; + outer: while(true) { + int n = in.read(a,size,left); + if( n == -1 ) { + if( size == 0 ) { + socket.close(); + return; + } + throw new IOException("unexpected end of input at "+size); + } + size += n; + for( int i=0; i<=size-4; i++ ) { + if( a[i]=='\r' && a[i+1]=='\n' && a[i+2]=='\r' && a[i+3]=='\n' ) { + endOfHeader = i + 4; + break outer; + } + } + left -= n; + if( left == 0 ) { + byte[] a2 = new byte[2*a.length]; + System.arraycopy(a,0,a2,0,size); + a = a2; + left = a.length - size; + } + } + rawHead = new String(a,0,endOfHeader); + //System.out.println(rawHead); + request.rawHead = rawHead; + RequestParser parser = new RequestParser(request); + parser.parseHead(); + + String lenStr = (String)request.headers.get("content-length"); + if( lenStr != null ) { + int len = Integer.parseInt(lenStr); + byte[] body = new byte[len]; + size -= endOfHeader; + System.arraycopy(a,endOfHeader,body,0,size); + while( size < len ) { + int n = in.read(body,size,len-size); if( n == -1 ) { - if( size == 0 ) { - socket.close(); - return; - } throw new IOException("unexpected end of input at "+size); } size += n; - for( int i=0; i<=size-4; i++ ) { - if( a[i]=='\r' && a[i+1]=='\n' && a[i+2]=='\r' && a[i+3]=='\n' ) { - endOfHeader = i + 4; - break outer; - } - } - left -= n; - if( left == 0 ) { - byte[] a2 = new byte[2*a.length]; - System.arraycopy(a,0,a2,0,size); - a = a2; - left = a.length - size; - } } - rawHead = new String(a,0,endOfHeader); - //System.out.println(rawHead); - request.rawHead = rawHead; - RequestParser parser = new RequestParser(request); - parser.parseHead(); - - String lenStr = (String)request.headers.get("content-length"); - if( lenStr != null ) { - int len = Integer.parseInt(lenStr); - byte[] body = new byte[len]; - size -= endOfHeader; - System.arraycopy(a,endOfHeader,body,0,size); - while( size < len ) { - int n = in.read(body,size,len-size); - if( n == -1 ) { - throw new IOException("unexpected end of input at "+size); - } - size += n; - } - request.body = body; - //System.out.println(new String(request.body)); + request.body = body; + //System.out.println(new String(request.body)); + } + + contentType = (String)request.headers.get("content-type"); + if( contentType != null ) { + contentType = contentType.toLowerCase(); + if( contentType.equals("application/x-www-form-urlencoded") ) { + parser.parseUrlencoded(null); + } else if( contentType.equals("application/x-www-form-urlencoded; charset=utf-8") ) { + parser.parseUrlencoded("utf-8"); + } else if( contentType.startsWith("multipart/form-data;") ) { + parser.parseMultipart(); + } else if( contentType.equals("application/json") ) { + parser.parseJson(null); + } else if( contentType.equals("application/json; charset=utf-8") ) { + parser.parseJson("utf-8"); + } else { + logger.info("unknown request content-type: "+contentType); } - - contentType = (String)request.headers.get("content-type"); - if( contentType != null ) { - contentType = contentType.toLowerCase(); - if( contentType.equals("application/x-www-form-urlencoded") ) { - parser.parseUrlencoded(null); - } else if( contentType.equals("application/x-www-form-urlencoded; charset=utf-8") ) { - parser.parseUrlencoded("utf-8"); - } else if( contentType.startsWith("multipart/form-data;") ) { - parser.parseMultipart(); - } else if( contentType.equals("application/json") ) { - parser.parseJson(null); - } else if( contentType.equals("application/json; charset=utf-8") ) { - parser.parseJson("utf-8"); - } else { - logger.info("unknown request content-type: "+contentType); - } - } + } + + String scheme = (String)request.headers.get("x-forwarded-proto"); + if( scheme != null ) + request.scheme = scheme; - String scheme = (String)request.headers.get("x-forwarded-proto"); - if( scheme != null ) - request.scheme = scheme; + if( "text/event-stream".equals(request.headers.get("accept")) ) { + ServerSentEvents.add(socket,request); + return; } + response = server.handler.handle(request); } catch(ParseException e) { logger.warn("parse error\n"+request.rawHead.trim()+"\n",e);