changeset 4:108812b08e75

add http/postRequest.k http/url.k
author Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp>
date Sun, 20 May 2012 21:09:03 +0900
parents 15c4de0e2db2
children f78fb53ef7b6 ed611531a49b
files http/postRequest.k http/url.k
diffstat 2 files changed, 206 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/http/postRequest.k	Sun May 20 21:09:03 2012 +0900
@@ -0,0 +1,82 @@
+using konoha.io.*;
+using konoha.socket.*;
+
+
+class HttpRequest()
+{
+	int port;
+	String method, uri;
+ 	String host, connection, charset, cacheControl, language;
+	Socket sock;
+	InputStream in;
+	OutputStream out;
+	HttpRequest() {
+		this.port = 80;
+		this.method = "POST";
+		this.uri = "index.html";
+		this.host = "";
+		this.charset = "UTF-8";
+		this.connection = "close";
+		this.cacheControl = "no-cache";
+		this.language = "en";
+	}
+	void setMethod(String method) {
+		this.method = method;
+	}
+	void setHost(String host) {
+		this.host = host;
+	}
+	void setUri(String uri) {
+		this.uri = uri;
+	}
+	void connectToHost() {
+		print("connect to "+ host);
+		this.sock = new Socket(this.host, port);
+		in = this.sock.getInputStream();
+		out = this.sock.getOutputStream();
+	}
+
+	void sendRequest() {
+		out <<< method + " /" + uri + " HTTP/1.1" <<< EOL;
+		out <<< "HOST: " + this.host <<< EOL;
+		out <<< "Connection: " + connection <<< EOL;
+		out <<< "Accept-Charset: " + charset <<< EOL;
+		out <<< "Cache-Control: " + cacheControl <<< EOL;
+		out <<< "Accept-Language: " + language <<< EOL;
+		out <<< EOL;
+		out.flush();
+	}
+
+	void printRequest() {
+		print("printRequest");
+		print(method + " /" + uri + " HTTP/1.1");
+		print("HOST: " + host);
+		print("Connection: " + connection);
+		print("Accept-Charset: " + charset);
+		print("Cache-Control: " + cacheControl);
+		print("Accept-Language: " + language);
+		print("");
+	}
+
+	void printResponse() {
+		print("print Response");
+		while ( !in.isClosed() ) {
+			String ret = in.readLine();
+			print(ret);
+		}
+	}
+
+}
+
+
+
+void main(String[] args)
+{
+	HttpRequest h = new HttpRequest();
+	h.setHost("dimolto.cr.ie.u-ryukyu.ac.jp");
+	h.setMethod("POST");
+	h.connectToHost();
+	h.sendRequest();
+	h.printRequest();
+	h.printResponse();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/http/url.k	Sun May 20 21:09:03 2012 +0900
@@ -0,0 +1,124 @@
+using konoha.socket.*;
+using konoha.io.*;
+
+class HttpRequest {
+
+	int keySize = 0;
+	String[] keys;
+	Map<String,String> property = {};
+	String host, method, uri;
+	String body;
+	Socket socket;
+	OutputStream out;
+	InputStream in;
+
+	HttpRequest() {
+		property = {}; 
+		this.method = "GET";
+	}
+	~HttpRequest() {
+		print("destructor");
+	}
+
+	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);
+		this.out = this.socket.getOutputStream();
+		this.in = this.socket.getInputStream();
+	}
+
+	void setRequestProperty(String key, String value) {
+		String[] tmpKey = new String[keySize+1];
+		for (int i=0; i < keySize; i++) {
+			tmpKey[i] = keys[i];
+		}
+		tmpKey[keySize] = key;
+		keys = tmpKey;
+		keySize++;
+
+		property.set(key, value);
+	}
+
+	void printRequestProperty() {
+		foreach (String key in keys)
+			OUT << key +": " + property[key] << EOL;
+	}
+
+	void setBody(String body) {
+		this.body = body;
+	}
+
+	void appendBody() {
+		String 
+	}
+
+	void sendRequest() {
+		out <<< this.method + " /" + this.uri + " HTTP/1.1" <<< EOL;
+		out <<<  "HOST: " + this.host <<< EOL;
+		for (String key : keys) {
+			out <<< key +": " + property[key] <<< EOL;
+		}
+		out <<< EOL;		
+		out <<< body <<< EOL;
+		out.flush();
+	}
+	
+
+	OutputStream getOutputStream() {
+		return this.out;
+	}
+
+	InputStream getInputStream() {
+		return this.in;
+	}
+
+	void close() {
+		out.close();
+		in.close();
+	}
+
+}
+
+
+
+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.sendRequest();
+
+	InputStream in = r.getInputStream();
+	printResponse(in);
+
+	r.close();
+
+/*
+
+	String http = "http://dimolto.cr.ie.u-ryukyu.ac.jp";
+	String split =  http.split("http://")[1];
+	print split;
+*/	
+}
\ No newline at end of file