# HG changeset patch # User Nobuyasu Oshiro # Date 1338216975 -32400 # Node ID 0335cdd081d06f076f33bdc3f90a3c96fe2eb5e6 # Parent da5149cbb9f41847ece3e3a37e18c1664c38f62a modify HttpRequest.k diff -r da5149cbb9f4 -r 0335cdd081d0 http/HttpRequest.k --- 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 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 diff -r da5149cbb9f4 -r 0335cdd081d0 http/httpRequestTest.k --- /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 diff -r da5149cbb9f4 -r 0335cdd081d0 http/test/urlTest.k --- 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 diff -r da5149cbb9f4 -r 0335cdd081d0 http/url.k --- 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