# HG changeset patch # User Nobuyasu Oshiro # Date 1337515743 -32400 # Node ID 108812b08e753d2d63c0a836839704efb4670246 # Parent 15c4de0e2db2a37a6699784274cb27ccf6de5267 add http/postRequest.k http/url.k diff -r 15c4de0e2db2 -r 108812b08e75 http/postRequest.k --- /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 diff -r 15c4de0e2db2 -r 108812b08e75 http/url.k --- /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 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