Mercurial Hosting > luan
annotate src/goodjava/webserver/Response.java @ 1708:8d257d56dc42
minor
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 04 Jul 2022 14:06:59 -0600 |
parents | fa066aaa068c |
children |
rev | line source |
---|---|
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
1 package goodjava.webserver; |
1137 | 2 |
3 import java.io.InputStream; | |
1161 | 4 import java.io.PrintWriter; |
1137 | 5 import java.util.Map; |
6 import java.util.LinkedHashMap; | |
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 | 9 import goodjava.util.CaseInsensitiveMap; |
1137 | 10 |
11 | |
12 public class Response { | |
13 public final String protocol = "HTTP/1.1"; | |
14 public volatile Status status = Status.OK; | |
1607 | 15 public final Map<String,Object> headers = Collections.synchronizedMap(new CaseInsensitiveMap<Object>(new LinkedHashMap<String,CaseInsensitiveMap.Value<Object>>())); |
1137 | 16 { |
1607 | 17 headers.put("Server","goodjava"); |
1137 | 18 } |
1160 | 19 private static final Body empty = new Body(0,new InputStream(){ |
20 public int read() { return -1; } | |
21 }); | |
22 public volatile Body body = empty; | |
1137 | 23 |
24 public static class Body { | |
25 public final long length; | |
26 public final InputStream content; | |
27 | |
28 public Body(long length,InputStream content) { | |
29 this.length = length; | |
30 this.content = content; | |
31 } | |
32 } | |
33 | |
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 | 54 public String toHeaderString() { |
55 StringBuilder sb = new StringBuilder(); | |
56 sb.append( protocol ) | |
57 .append( ' ' ).append( status.code ) | |
58 .append( ' ' ).append( status.reason ) | |
59 .append( "\r\n" ) | |
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 | 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 | 71 } |
72 sb.append( "\r\n" ); | |
73 return sb.toString(); | |
74 } | |
1161 | 75 |
76 | |
77 public static Response errorResponse(Status status,String text) { | |
78 Response response = new Response(); | |
79 response.status = status; | |
1607 | 80 response.headers.put( "Content-Type", "text/plain; charset=utf-8" ); |
1161 | 81 PrintWriter writer = new PrintWriter( new ResponseOutputStream(response) ); |
82 writer.write( text ); | |
83 writer.close(); | |
84 return response; | |
85 } | |
1137 | 86 } |