Mercurial > hg > Members > nobuyasu > konoha
changeset 12:da5149cbb9f4 draft
add http/test/urlTest.k
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Mon, 28 May 2012 22:39:46 +0900 |
parents | f2fc4689d3d6 |
children | 0335cdd081d0 |
files | http/HttpRequest.k http/test/urlTest.k http/url.k test/throwTest.k |
diffstat | 4 files changed, 45 insertions(+), 126 deletions(-) [+] |
line wrap: on
line diff
--- a/http/HttpRequest.k Sun May 27 21:08:36 2012 +0900 +++ b/http/HttpRequest.k Mon May 28 22:39:46 2012 +0900 @@ -64,13 +64,17 @@ out <<< this.method + " /" + this.uri + " HTTP/1.1" <<< EOL; out <<< "HOST: " + this.host <<< EOL; for (String key : property.keys()) { - OUT <<< key +": " + property[key] <<< EOL; + out <<< key +": " + property[key] <<< EOL; } out <<< EOL; out <<< body <<< EOL; out.flush(); } + int getBodySize() { + return this.body.getSize(); + } + OutputStream getOutputStream() { return this.out; } @@ -109,7 +113,6 @@ InputStream in = r.getInputStream(); printResponse(in); - r.close();
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/http/test/urlTest.k Mon May 28 22:39:46 2012 +0900 @@ -0,0 +1,20 @@ +include "../URL.k"; + + +void main(String[] args) +{ + String url = "http://google.com"; + print url; + URL u; + try { + u = new URL(url); + } catch(Script!! e) { + OUT << "catch Script!!" <<< EOL; + OUT << e << EOL; + } + print u.getHost(); + + + + +} \ No newline at end of file
--- a/http/url.k Sun May 27 21:08:36 2012 +0900 +++ b/http/url.k Mon May 28 22:39:46 2012 +0900 @@ -1,121 +1,17 @@ -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"; - } - - 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(); - } +class URL { - void setRequestProperty(String key, String value) { - String[] tmpKey = new String[keySize+1]; - for (int i=0; i < keySize; i++) { - tmpKey[i] = keys[i]; + String url; + String host; + URL(String url) { + int ret = url.search("http://"); + if (ret == 0) { + this.host = url.split("http://")[1]; + this.url = url; + } else { + throw new Script!!("unknown protocol"); } - 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
--- a/test/throwTest.k Sun May 27 21:08:36 2012 +0900 +++ b/test/throwTest.k Mon May 28 22:39:46 2012 +0900 @@ -1,20 +1,20 @@ void main(String[] args) { try { - // throw new Script!!("throw Script!!"); - // throw new IO!!("throw IO!!"); - throw new MyError!!("throw MyError!!"); + // throw new Script!!("throw Script!!"); +// throw new IO!!("throw IO!!"); + throw new MyError!!("throw MyError!!"); } catch (Script!! e) { - print "catch Script!!"; - print e; + print "catch Script!!"; + print e; } catch (IO!! e) { - print "catch IO!!"; - print e; + print "catch IO!!"; + print e; } catch (MyError!! e) { - print "catch MyError!!"; - print e; + print "catch MyError!!"; + print e; } finally { - + print "finally" }