# HG changeset patch # User xitiomet # Date 1322693423 18000 # Node ID 41d9c6df6f8558ef3684b5cdeb04cb547c3c38f5 # Parent 2a9a5abb86c8de40ffcaa1096026e31adabeca0e interactive response diff -r 2a9a5abb86c8de40ffcaa1096026e31adabeca0e -r 41d9c6df6f8558ef3684b5cdeb04cb547c3c38f5 src/org/openstatic/http/HttpRequest.java --- a/src/org/openstatic/http/HttpRequest.java Sat Nov 26 15:23:54 2011 -0500 +++ b/src/org/openstatic/http/HttpRequest.java Wed Nov 30 17:50:23 2011 -0500 @@ -21,6 +21,7 @@ import java.net.Socket; import java.io.OutputStream; import java.io.InputStream; +import java.io.BufferedReader; import java.util.Date; import java.util.Enumeration; import java.util.Hashtable; @@ -32,25 +33,26 @@ public class HttpRequest { private Hashtable headers; - private Hashtable outbound_headers; + protected Hashtable outbound_headers; private Hashtable cookies; private Hashtable post_data; private Hashtable get_data; private String raw_post; private String raw_get; - private Socket socket; - private OutputStream socket_output_stream; + protected Socket socket; + protected OutputStream socket_output_stream; + protected BufferedReader socket_reader; private String path; private String request_type; private String request_prefix; private String set_cookie; - private String clientHostname; - private String serverHostname; - private PlaceboHttpServer myServer; + protected String clientHostname; + protected String serverHostname; + protected PlaceboHttpServer myServer; private PlaceboSession mySession; - protected HttpRequest(String path, String request_type, Hashtable headers, Hashtable cookies, Hashtable post_data, String raw_post, Hashtable get_data, String raw_get, Socket socket, PlaceboHttpServer myServer) + protected HttpRequest(String path, String request_type, Hashtable headers, Hashtable cookies, Hashtable post_data, String raw_post, Hashtable get_data, String raw_get, Socket socket, PlaceboHttpServer myServer, BufferedReader br) { this.path = path; this.request_prefix = null; @@ -71,6 +73,7 @@ try { this.socket_output_stream = socket.getOutputStream(); + this.socket_reader = br; } catch (Exception e) { this.myServer.logln(this.clientHostname, "Failed Initialization of output stream"); } diff -r 2a9a5abb86c8de40ffcaa1096026e31adabeca0e -r 41d9c6df6f8558ef3684b5cdeb04cb547c3c38f5 src/org/openstatic/http/HttpRequestThread.java --- a/src/org/openstatic/http/HttpRequestThread.java Sat Nov 26 15:23:54 2011 -0500 +++ b/src/org/openstatic/http/HttpRequestThread.java Wed Nov 30 17:50:23 2011 -0500 @@ -198,7 +198,7 @@ if (request_type != null) { - HttpRequest req_obj = new HttpRequest(request_path, request_type, headers, cookies, formContent, raw_form_data, urlGetParameters, get_params, this.connection, this.myServer); + HttpRequest req_obj = new HttpRequest(request_path, request_type, headers, cookies, formContent, raw_form_data, urlGetParameters, get_params, this.connection, this.myServer, br); this.myServer.routeRequest(req_obj); } } catch (Exception x) {} diff -r 2a9a5abb86c8de40ffcaa1096026e31adabeca0e -r 41d9c6df6f8558ef3684b5cdeb04cb547c3c38f5 src/org/openstatic/http/InteractiveResponse.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/org/openstatic/http/InteractiveResponse.java Wed Nov 30 17:50:23 2011 -0500 @@ -0,0 +1,96 @@ +package org.openstatic.http; + +import java.io.BufferedReader; +import java.io.OutputStream; +import java.io.InputStream; +import java.io.IOException; +import java.net.Socket; +import java.util.Enumeration; +import java.util.Hashtable; +import java.util.Date; +import org.json.*; + +public class InteractiveResponse +{ + private String contentType; + private BufferedReader socket_reader; + private OutputStream socket_output_stream; + private PlaceboHttpServer myServer; + protected Hashtable outbound_headers; + protected String clientHostname; + protected Socket socket; + private boolean connected; + + public InteractiveResponse(HttpRequest rq) + { + this.socket_reader = rq.socket_reader; + this.socket_output_stream = rq.socket_output_stream; + this.myServer = rq.myServer; + this.outbound_headers = rq.outbound_headers; + this.clientHostname = rq.clientHostname; + this.socket = rq.socket; + this.contentType = "text/javascript"; + this.push("HTTP/1.1 200 OK\r\n"); + this.push("Server: OpenstaticPlacebo/1.0\r\n"); + this.push("Date: " + (new Date()).toString() + "\r\n"); + this.push("Content-Type: " + InteractiveResponse.this.contentType + "\r\n"); + this.connected = true; + for(Enumeration names2 = InteractiveResponse.this.outbound_headers.keys(); names2.hasMoreElements(); ) + { + String c_name = names2.nextElement(); + String c_value = outbound_headers.get(c_name); + this.push(c_name + ": " + c_value + "\r\n"); + } + this.push("\r\n"); + } + + public boolean isConnected() + { + return this.socket.isConnected() && this.connected; + } + + /** Set the content type for this response object */ + public void setContentType(String type) + { + this.contentType = type; + } + + public String pull() + { + try + { + return this.socket_reader.readLine(); + } catch (Exception e) { + return null; + } + } + + public void push(JSONObject data) + { + push(data.toString() + "\r\n"); + } + + public void close() + { + try + { + this.socket_output_stream.close(); + this.socket.close(); + } catch (Exception close_error) { + this.myServer.logln(this.clientHostname, "Close Exception: " + close_error.toString() + " / " + close_error.getMessage()); + } + } + + public void push(String out) + { + try + { + this.socket_output_stream.write(out.getBytes()); + this.socket_output_stream.flush(); + this.myServer.logln(this.clientHostname, "<- " + out); + } catch (Exception we) { + this.myServer.logln(this.clientHostname, "