changeset 13:0335cdd081d0 draft

modify HttpRequest.k
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Mon, 28 May 2012 23:56:15 +0900
parents da5149cbb9f4
children bc647a5f0421
files http/HttpRequest.k http/httpRequestTest.k http/test/urlTest.k http/url.k
diffstat 4 files changed, 90 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/http/HttpRequest.k	Mon May 28 22:39:46 2012 +0900
+++ b/http/HttpRequest.k	Mon May 28 23:56:15 2012 +0900
@@ -1,34 +1,31 @@
+include "URL.k"
+
 using konoha.socket.*;
 using konoha.io.*;
 
 class HttpRequest {
 
-	String httpVersion;
+	URL u;
 	Map<String,String> property = {};
 	static final String httpVersion = "HTTP/1.1";
-	static final String acceptString = "text/html";
-	String host, method, uri;
+	static final int PORT = 80;
+	String url, host, method, uri;
 	String body;
 	Socket socket;
 	OutputStream out;
 	InputStream in;
 
-	HttpRequest() {
-		property = {}; 
-		this.method = "GET";
-		this.httpVersion = "HTTP/1.1"
+	HttpRequest(URL u) {
+		this.u = u;
+		property = {}; // Map instance must is initialize here
+		this.method = "POST"; // default method is POST
+		this.url = u.getUrl();
+		this.host = u.getHost();
+		this.uri = u.getUri();
 	}
 
-	void setUri(String uri) {
-		this.uri = uri;
-	}
-	void setMethod(String method) {
-		this.method = method;
-	}
-
-	void openConnection(String host, int port=80) {
-		this.host = host;
-		this.socket = new Socket(host, port);
+	void openConnection(int port=80) {
+		this.socket = new Socket(this.host, port);
 		this.out = this.socket.getOutputStream();
 		this.in = this.socket.getInputStream();
 	}
@@ -50,27 +47,28 @@
 		this.body = this.body + str;
 	}
 
-	void printRequest() {
-		OUT <<< this.method + " /" + this.uri + " " + this.httpVersion<<< EOL;
-		OUT <<<  "HOST: " + this.host <<< EOL;
-		for (String key : property.keys()) {
-			OUT <<< key +": " + property[key] <<< EOL;
-		}
-		OUT <<< EOL;		
- 		OUT <<< body <<< EOL;
-	}
-
-	void writeRequest() {
-		out <<< this.method + " /" + this.uri + " HTTP/1.1" <<< EOL;
+	void output(OutputRequest out) {
+		out <<< this.method + " " + this.uri + " " + this.httpVersion <<< EOL;
 		out <<<  "HOST: " + this.host <<< EOL;
 		for (String key : property.keys()) {
 			out <<< key +": " + property[key] <<< EOL;
 		}
+		if (body.getSize() != 0) {
+			out <<<  "Content-length: " + this.body.getSize() <<< EOL;			
+		}
 		out <<< EOL;		
 		out <<< body <<< EOL;
 		out.flush();
 	}
 
+	void printRequest() {
+		output(OUT);
+	}
+
+	void writeRequest() {
+		output(this.out);
+	}
+
 	int getBodySize() {
 		return this.body.getSize();
 	}
@@ -89,31 +87,3 @@
 	}
 
 }
-
-void printResponse(InputStream in) {
-	print("print Response");
-	while ( !in.isClosed() ) {
-		String ret = in.readLine();
-		OUT << ret << EOL;
-	}
-}
-
-
-void main(String[] args)
-{
-	HttpRequest r = new HttpRequest();
-	r.openConnection("localhost");
-	r.setMethod("POST");
-	r.setUri("index.html");
-	r.setRequestProperty("Connection","close");
-	r.setRequestProperty("Accept-Charset","UTF-8");
-	r.setRequestProperty("Cache-Control","no-cache");
-	r.setRequestProperty("Accept-Language","en");
-	r.writeRequest();
-
-    InputStream in = r.getInputStream();
-	printResponse(in);
-	r.close();
-
-
-}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/http/httpRequestTest.k	Mon May 28 23:56:15 2012 +0900
@@ -0,0 +1,34 @@
+include "HttpRequest.k"
+
+
+
+void printResponse(InputStream in) {
+	print("print Response");
+	while ( !in.isClosed() ) {
+		String ret = in.readLine();
+		OUT << ret << EOL;
+	}
+}
+
+
+void main(String[] args)
+{
+	URL url = new URL("http://dimolto.cr.ie.u-ryukyu.ac.jp/~aotokage/post.php");
+	HttpRequest r = new HttpRequest(url);
+	r.openConnection();
+	r.setMethod("POST");
+	r.setRequestProperty("Connection","close");
+	r.setRequestProperty("Accept-Charset","UTF-8");
+	r.setRequestProperty("Cache-Control","no-cache");
+	r.setRequestProperty("Accept-Language","en");
+	r.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
+	r.setBody("sub=AAAAAAAA&txt=BBB");
+	r.writeRequest();
+	r.printRequest();
+
+
+    InputStream in = r.getInputStream();
+	printResponse(in);
+	r.close();
+
+}
\ No newline at end of file
--- a/http/test/urlTest.k	Mon May 28 22:39:46 2012 +0900
+++ b/http/test/urlTest.k	Mon May 28 23:56:15 2012 +0900
@@ -3,7 +3,7 @@
 
 void main(String[] args)
 {
-	String url = "http://google.com";
+	String url = "http://ie.u-ryukyu.ac.jp/~kono/lecture/index.html";
 	print url;
 	URL u;
 	try {
@@ -11,10 +11,13 @@
 	} catch(Script!! e) {
 		OUT << "catch Script!!" <<< EOL;
 		OUT << e << EOL;
+		System.exit(0);
 	}
+	print u.getURL();
 	print u.getHost();
+	print u.getUri();
 
-
+	
 
 
 }
\ No newline at end of file
--- a/http/url.k	Mon May 28 22:39:46 2012 +0900
+++ b/http/url.k	Mon May 28 23:56:15 2012 +0900
@@ -2,16 +2,34 @@
 
 	String url;
 	String host;
+	String uri;
+
+	String copyString(String str, int index) {
+		int size = str.getSize();
+		String tmp = "";
+		for (int i=index; i < size; i++) {
+			tmp += str[i];
+		}
+		return tmp;
+	}
+
+
 	URL(String url) {
 		int ret = url.search("http://");
-		if (ret == 0) {
-			this.host = url.split("http://")[1];
-			this.url = url;
-		} else {
+		if ( ret != 0) {
 			throw new Script!!("unknown protocol");
 		}
+		this.url = url;
+		String str = url.split("http://")[1];
+		ret = str.search("/");
+		if ( ret == -1) { 
+			this.uri = "/";
+			this.host = str;
+		} else { 
+			print "ret = " + ret  + this.host;
+			this.host = str.split("/")[0];
+			this.uri = copyString(str, ret);
+		}
 	}
-	
-
 
 }
\ No newline at end of file