Mercurial Hosting > luan
annotate src/goodjava/webserver/Response.java @ 1579:dd881eb03d87
yourkit link
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 10 Feb 2021 22:49:47 -0700 |
parents | 27efb1fcbcb5 |
children | ef9d96aeb541 |
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; |
1137 | 9 |
10 | |
11 public class Response { | |
12 public final String protocol = "HTTP/1.1"; | |
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 | 15 { |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1347
diff
changeset
|
16 headers.put("server","Goodjava"); |
1137 | 17 } |
1160 | 18 private static final Body empty = new Body(0,new InputStream(){ |
19 public int read() { return -1; } | |
20 }); | |
21 public volatile Body body = empty; | |
1137 | 22 |
23 public static class Body { | |
24 public final long length; | |
25 public final InputStream content; | |
26 | |
27 public Body(long length,InputStream content) { | |
28 this.length = length; | |
29 this.content = content; | |
30 } | |
31 } | |
32 | |
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 | 53 public String toHeaderString() { |
54 StringBuilder sb = new StringBuilder(); | |
55 sb.append( protocol ) | |
56 .append( ' ' ).append( status.code ) | |
57 .append( ' ' ).append( status.reason ) | |
58 .append( "\r\n" ) | |
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 | 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 | 70 } |
71 sb.append( "\r\n" ); | |
72 return sb.toString(); | |
73 } | |
1161 | 74 |
75 | |
76 public static Response errorResponse(Status status,String text) { | |
77 Response response = new Response(); | |
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 | 80 PrintWriter writer = new PrintWriter( new ResponseOutputStream(response) ); |
81 writer.write( text ); | |
82 writer.close(); | |
83 return response; | |
84 } | |
1137 | 85 } |