annotate src/goodjava/webserver/Response.java @ 1718:5603ee8e2a71

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