Install some basic error handling in Dispatch.
[jrw.git] / src / jrw / Request.java
CommitLineData
3e20c35c
FT
1package jrw;
2
3import java.util.*;
4
5public class Request {
6 public final Map<Object, Object> env;
7 public final Map<Object, Object> resp = new HashMap<>();
ad84ba3b
FT
8 public String status = "200 OK";
9 public Object body = null;
3e20c35c
FT
10
11 public Request(Map<Object, Object> env) {
12 this.env = env;
13 }
14
963aee0d
FT
15 public String method() {return((String)env.get("REQUEST_METHOD"));}
16 public String remoteaddr() {return((String)env.get("REMOTE_ADDR"));}
17 public String serverport() {return((String)env.get("SERVER_PORT"));}
18 public String servername() {return((String)env.get("SERVER_NAME"));}
19 public String uri() {return((String)env.get("REQUEST_URI"));}
20 public String uriname() {return((String)env.get("SCRIPT_NAME"));}
21 public String filename() {return((String)env.get("SCRIPT_FILENAME"));}
22 public String pathinfo() {return((String)env.get("PATH_INFO"));}
23 public String query() {return((String)env.get("QUERY_STRING"));}
24 public boolean https() {return(env.containsKey("HTTPS"));}
25
3e20c35c
FT
26 public String ihead(String name, String def) {
27 StringBuilder buf = new StringBuilder();
28 buf.append("HTTP_");
29 for(int i = 0; i < name.length(); i++) {
30 char c = name.charAt(i);
31 if(c == '-')
32 buf.append('_');
33 else if((c >= 'a') && (c <= 'z'))
34 buf.append((char)(c + ('A' - 'a')));
35 else
36 buf.append(c);
37 }
38 Object ret = env.get(buf.toString());
39 if(ret instanceof String)
40 return((String)ret);
41 return(def);
42 }
43
44 @SuppressWarnings("unchecked")
ad84ba3b 45 public Request ohead(String name, Object val, boolean repl) {
3e20c35c
FT
46 name = "http." + name;
47 if(repl) {
48 resp.put(name, val);
49 } else {
50 Object cur = resp.get(name);
51 if(cur == null)
52 resp.put(name, val);
53 else if(cur instanceof Collection)
54 ((Collection)cur).add(val);
55 else
56 resp.put(name, new ArrayList<Object>(Arrays.asList(cur, val)));
57 }
ad84ba3b 58 return(this);
3e20c35c
FT
59 }
60
ad84ba3b
FT
61 public Request status(String status) {this.status = status; return(this);}
62 public Request body(Object body) {this.body = body; return(this);}
63
3e20c35c 64 public Map<Object, Object> response() {
ad84ba3b
FT
65 resp.put("http.status", status);
66 resp.put("jagi.output", body);
3e20c35c
FT
67 return(resp);
68 }
69}