annotate src/goodjava/webserver/Response.java @ 1474:13cbce740e1e

LowercaseAnalyzer
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 17 Apr 2020 21:53:09 -0600
parents 27efb1fcbcb5
children ef9d96aeb541
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;
1161
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
4 import java.io.PrintWriter;
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 import java.util.Map;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.util.LinkedHashMap;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.util.Collections;
1146
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
8 import java.util.List;
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 public class Response {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 public final String protocol = "HTTP/1.1";
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 public volatile Status status = Status.OK;
1146
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
14 public final Map<String,Object> headers = Collections.synchronizedMap(new LinkedHashMap<String,Object>());
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 {
1402
27efb1fcbcb5 move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents: 1347
diff changeset
16 headers.put("server","Goodjava");
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 }
1160
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1148
diff changeset
18 private static final Body empty = new Body(0,new InputStream(){
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1148
diff changeset
19 public int read() { return -1; }
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1148
diff changeset
20 });
4beabb087be6 add http/impl
Franklin Schmidt <fschmidt@gmail.com>
parents: 1148
diff changeset
21 public volatile Body body = empty;
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 public static class Body {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 public final long length;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 public final InputStream content;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 public Body(long length,InputStream content) {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 this.length = length;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 this.content = content;
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33
1146
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
34 public void addHeader(String name,String value) {
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
35 Util.add(headers,name,value);
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
36 }
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
37
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
38 public void setCookie(String name,String value,Map<String,String> attributes) {
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
39 StringBuilder buf = new StringBuilder();
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
40 buf.append( Util.urlEncode(name) );
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
41 buf.append( '=' );
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
42 buf.append( Util.urlEncode(value) );
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
43 for( Map.Entry<String,String> entry : attributes.entrySet() ) {
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
44 buf.append( "; " );
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
45 buf.append( entry.getKey() );
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
46 buf.append( '=' );
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
47 buf.append( entry.getValue() );
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
48 }
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
49 addHeader( "Set-Cookie", buf.toString() );
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
50 }
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
51
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
52
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 public String toHeaderString() {
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 StringBuilder sb = new StringBuilder();
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 sb.append( protocol )
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 .append( ' ' ).append( status.code )
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 .append( ' ' ).append( status.reason )
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 .append( "\r\n" )
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 ;
1146
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
60 for( Map.Entry<String,Object> entry : headers.entrySet() ) {
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 String name = entry.getKey();
1146
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
62 Object value = entry.getValue();
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
63 if( value instanceof List ) {
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
64 for( Object v : (List)value ) {
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
65 sb.append( name ).append( ": " ).append( v ).append( "\r\n" );
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
66 }
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
67 } else {
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
68 sb.append( name ).append( ": " ).append( value ).append( "\r\n" );
2dda3c92a473 webserver - implement cookies
Franklin Schmidt <fschmidt@gmail.com>
parents: 1137
diff changeset
69 }
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 }
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 sb.append( "\r\n" );
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 return sb.toString();
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 }
1161
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
74
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
75
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
76 public static Response errorResponse(Status status,String text) {
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
77 Response response = new Response();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
78 response.status = status;
1165
668f29bc52ea clean up content-type
Franklin Schmidt <fschmidt@gmail.com>
parents: 1161
diff changeset
79 response.headers.put( "content-type", "text/plain; charset=utf-8" );
1161
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
80 PrintWriter writer = new PrintWriter( new ResponseOutputStream(response) );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
81 writer.write( text );
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
82 writer.close();
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
83 return response;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1160
diff changeset
84 }
1137
c123ee15f99b add webserver
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 }