comparison 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
comparison
equal deleted inserted replaced
1737:6c9aea554691 1738:9713f7fd50b3
30 try { 30 try {
31 Request request = new Request(); 31 Request request = new Request();
32 Response response; 32 Response response;
33 String contentType = null; 33 String contentType = null;
34 try { 34 try {
35 { 35 InputStream in = socket.getInputStream();
36 InputStream in = socket.getInputStream(); 36 byte[] a = new byte[8192];
37 byte[] a = new byte[8192]; 37 int endOfHeader;
38 int endOfHeader; 38 int size = 0;
39 int size = 0; 39 int left = a.length;
40 int left = a.length; 40 outer: while(true) {
41 outer: while(true) { 41 int n = in.read(a,size,left);
42 int n = in.read(a,size,left); 42 if( n == -1 ) {
43 if( size == 0 ) {
44 socket.close();
45 return;
46 }
47 throw new IOException("unexpected end of input at "+size);
48 }
49 size += n;
50 for( int i=0; i<=size-4; i++ ) {
51 if( a[i]=='\r' && a[i+1]=='\n' && a[i+2]=='\r' && a[i+3]=='\n' ) {
52 endOfHeader = i + 4;
53 break outer;
54 }
55 }
56 left -= n;
57 if( left == 0 ) {
58 byte[] a2 = new byte[2*a.length];
59 System.arraycopy(a,0,a2,0,size);
60 a = a2;
61 left = a.length - size;
62 }
63 }
64 rawHead = new String(a,0,endOfHeader);
65 //System.out.println(rawHead);
66 request.rawHead = rawHead;
67 RequestParser parser = new RequestParser(request);
68 parser.parseHead();
69
70 String lenStr = (String)request.headers.get("content-length");
71 if( lenStr != null ) {
72 int len = Integer.parseInt(lenStr);
73 byte[] body = new byte[len];
74 size -= endOfHeader;
75 System.arraycopy(a,endOfHeader,body,0,size);
76 while( size < len ) {
77 int n = in.read(body,size,len-size);
43 if( n == -1 ) { 78 if( n == -1 ) {
44 if( size == 0 ) {
45 socket.close();
46 return;
47 }
48 throw new IOException("unexpected end of input at "+size); 79 throw new IOException("unexpected end of input at "+size);
49 } 80 }
50 size += n; 81 size += n;
51 for( int i=0; i<=size-4; i++ ) {
52 if( a[i]=='\r' && a[i+1]=='\n' && a[i+2]=='\r' && a[i+3]=='\n' ) {
53 endOfHeader = i + 4;
54 break outer;
55 }
56 }
57 left -= n;
58 if( left == 0 ) {
59 byte[] a2 = new byte[2*a.length];
60 System.arraycopy(a,0,a2,0,size);
61 a = a2;
62 left = a.length - size;
63 }
64 } 82 }
65 rawHead = new String(a,0,endOfHeader); 83 request.body = body;
66 //System.out.println(rawHead); 84 //System.out.println(new String(request.body));
67 request.rawHead = rawHead; 85 }
68 RequestParser parser = new RequestParser(request); 86
69 parser.parseHead(); 87 contentType = (String)request.headers.get("content-type");
70 88 if( contentType != null ) {
71 String lenStr = (String)request.headers.get("content-length"); 89 contentType = contentType.toLowerCase();
72 if( lenStr != null ) { 90 if( contentType.equals("application/x-www-form-urlencoded") ) {
73 int len = Integer.parseInt(lenStr); 91 parser.parseUrlencoded(null);
74 byte[] body = new byte[len]; 92 } else if( contentType.equals("application/x-www-form-urlencoded; charset=utf-8") ) {
75 size -= endOfHeader; 93 parser.parseUrlencoded("utf-8");
76 System.arraycopy(a,endOfHeader,body,0,size); 94 } else if( contentType.startsWith("multipart/form-data;") ) {
77 while( size < len ) { 95 parser.parseMultipart();
78 int n = in.read(body,size,len-size); 96 } else if( contentType.equals("application/json") ) {
79 if( n == -1 ) { 97 parser.parseJson(null);
80 throw new IOException("unexpected end of input at "+size); 98 } else if( contentType.equals("application/json; charset=utf-8") ) {
81 } 99 parser.parseJson("utf-8");
82 size += n; 100 } else {
83 } 101 logger.info("unknown request content-type: "+contentType);
84 request.body = body;
85 //System.out.println(new String(request.body));
86 } 102 }
87 103 }
88 contentType = (String)request.headers.get("content-type");
89 if( contentType != null ) {
90 contentType = contentType.toLowerCase();
91 if( contentType.equals("application/x-www-form-urlencoded") ) {
92 parser.parseUrlencoded(null);
93 } else if( contentType.equals("application/x-www-form-urlencoded; charset=utf-8") ) {
94 parser.parseUrlencoded("utf-8");
95 } else if( contentType.startsWith("multipart/form-data;") ) {
96 parser.parseMultipart();
97 } else if( contentType.equals("application/json") ) {
98 parser.parseJson(null);
99 } else if( contentType.equals("application/json; charset=utf-8") ) {
100 parser.parseJson("utf-8");
101 } else {
102 logger.info("unknown request content-type: "+contentType);
103 }
104 }
105 104
106 String scheme = (String)request.headers.get("x-forwarded-proto"); 105 String scheme = (String)request.headers.get("x-forwarded-proto");
107 if( scheme != null ) 106 if( scheme != null )
108 request.scheme = scheme; 107 request.scheme = scheme;
108
109 if( "text/event-stream".equals(request.headers.get("accept")) ) {
110 ServerSentEvents.add(socket,request);
111 return;
109 } 112 }
113
110 response = server.handler.handle(request); 114 response = server.handler.handle(request);
111 } catch(ParseException e) { 115 } catch(ParseException e) {
112 logger.warn("parse error\n"+request.rawHead.trim()+"\n",e); 116 logger.warn("parse error\n"+request.rawHead.trim()+"\n",e);
113 String msg = e.toString(); 117 String msg = e.toString();
114 if( contentType != null ) 118 if( contentType != null )