comparison http/HttpRequest.k @ 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
comparison
equal deleted inserted replaced
12:da5149cbb9f4 13:0335cdd081d0
1 include "URL.k"
2
1 using konoha.socket.*; 3 using konoha.socket.*;
2 using konoha.io.*; 4 using konoha.io.*;
3 5
4 class HttpRequest { 6 class HttpRequest {
5 7
6 String httpVersion; 8 URL u;
7 Map<String,String> property = {}; 9 Map<String,String> property = {};
8 static final String httpVersion = "HTTP/1.1"; 10 static final String httpVersion = "HTTP/1.1";
9 static final String acceptString = "text/html"; 11 static final int PORT = 80;
10 String host, method, uri; 12 String url, host, method, uri;
11 String body; 13 String body;
12 Socket socket; 14 Socket socket;
13 OutputStream out; 15 OutputStream out;
14 InputStream in; 16 InputStream in;
15 17
16 HttpRequest() { 18 HttpRequest(URL u) {
17 property = {}; 19 this.u = u;
18 this.method = "GET"; 20 property = {}; // Map instance must is initialize here
19 this.httpVersion = "HTTP/1.1" 21 this.method = "POST"; // default method is POST
22 this.url = u.getUrl();
23 this.host = u.getHost();
24 this.uri = u.getUri();
20 } 25 }
21 26
22 void setUri(String uri) { 27 void openConnection(int port=80) {
23 this.uri = uri; 28 this.socket = new Socket(this.host, port);
24 }
25 void setMethod(String method) {
26 this.method = method;
27 }
28
29 void openConnection(String host, int port=80) {
30 this.host = host;
31 this.socket = new Socket(host, port);
32 this.out = this.socket.getOutputStream(); 29 this.out = this.socket.getOutputStream();
33 this.in = this.socket.getInputStream(); 30 this.in = this.socket.getInputStream();
34 } 31 }
35 32
36 void setRequestProperty(String key, String value) { 33 void setRequestProperty(String key, String value) {
48 45
49 void appendBody(String str) { 46 void appendBody(String str) {
50 this.body = this.body + str; 47 this.body = this.body + str;
51 } 48 }
52 49
53 void printRequest() { 50 void output(OutputRequest out) {
54 OUT <<< this.method + " /" + this.uri + " " + this.httpVersion<<< EOL; 51 out <<< this.method + " " + this.uri + " " + this.httpVersion <<< EOL;
55 OUT <<< "HOST: " + this.host <<< EOL;
56 for (String key : property.keys()) {
57 OUT <<< key +": " + property[key] <<< EOL;
58 }
59 OUT <<< EOL;
60 OUT <<< body <<< EOL;
61 }
62
63 void writeRequest() {
64 out <<< this.method + " /" + this.uri + " HTTP/1.1" <<< EOL;
65 out <<< "HOST: " + this.host <<< EOL; 52 out <<< "HOST: " + this.host <<< EOL;
66 for (String key : property.keys()) { 53 for (String key : property.keys()) {
67 out <<< key +": " + property[key] <<< EOL; 54 out <<< key +": " + property[key] <<< EOL;
68 } 55 }
56 if (body.getSize() != 0) {
57 out <<< "Content-length: " + this.body.getSize() <<< EOL;
58 }
69 out <<< EOL; 59 out <<< EOL;
70 out <<< body <<< EOL; 60 out <<< body <<< EOL;
71 out.flush(); 61 out.flush();
62 }
63
64 void printRequest() {
65 output(OUT);
66 }
67
68 void writeRequest() {
69 output(this.out);
72 } 70 }
73 71
74 int getBodySize() { 72 int getBodySize() {
75 return this.body.getSize(); 73 return this.body.getSize();
76 } 74 }
87 out.close(); 85 out.close();
88 in.close(); 86 in.close();
89 } 87 }
90 88
91 } 89 }
92
93 void printResponse(InputStream in) {
94 print("print Response");
95 while ( !in.isClosed() ) {
96 String ret = in.readLine();
97 OUT << ret << EOL;
98 }
99 }
100
101
102 void main(String[] args)
103 {
104 HttpRequest r = new HttpRequest();
105 r.openConnection("localhost");
106 r.setMethod("POST");
107 r.setUri("index.html");
108 r.setRequestProperty("Connection","close");
109 r.setRequestProperty("Accept-Charset","UTF-8");
110 r.setRequestProperty("Cache-Control","no-cache");
111 r.setRequestProperty("Accept-Language","en");
112 r.writeRequest();
113
114 InputStream in = r.getInputStream();
115 printResponse(in);
116 r.close();
117
118
119 }