1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.metricshub.wbem.sblim.cimclient.internal.http;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 import java.io.IOException;
49 import java.io.InputStream;
50 import java.net.HttpURLConnection;
51 import org.metricshub.wbem.sblim.cimclient.internal.http.io.ASCIIPrintStream;
52
53
54
55
56
57 public class HttpClientMethod extends HttpMethod {
58 private boolean iIncomming = true;
59
60 private String iHttpHeader;
61
62 private int iMinor, iMajor;
63
64 private int iStatus;
65
66 private String iMethod;
67
68 private String iRequest;
69
70 private String iResponse;
71
72
73
74
75
76
77
78
79
80
81
82
83
84 public HttpClientMethod(String pMethod, String pRequest, int pMajor, int pMinor) {
85 this.iMinor = pMinor;
86 this.iMajor = pMajor;
87 this.iRequest = pRequest;
88 this.iMethod = pMethod;
89 this.iIncomming = false;
90 }
91
92
93
94
95
96
97
98
99 public HttpClientMethod(InputStream pReader) throws IOException {
100 String line = null;
101
102 do {
103 line = readLine(pReader);
104 } while (line == null || line.length() == 0);
105 int rqt = line.indexOf(' ');
106 int prev = 0;
107 if (rqt > -1) {
108 int next = rqt;
109 this.iHttpHeader = line.substring(prev, next).toUpperCase();
110
111 prev = this.iHttpHeader.indexOf('/');
112 if (prev > 0 && this.iHttpHeader.substring(0, prev).equalsIgnoreCase("HTTP")) {
113 next = this.iHttpHeader.indexOf('.', prev + 1);
114 try {
115 this.iMajor = Integer.parseInt(this.iHttpHeader.substring(prev + 1, next));
116 this.iMinor = Integer.parseInt(this.iHttpHeader.substring(next + 1));
117 } catch (Exception e) {
118 throw new HttpException(HttpURLConnection.HTTP_BAD_METHOD, "Bad method");
119 }
120 prev = rqt;
121 rqt = line.indexOf(' ', prev + 1);
122 if (rqt > -1) {
123 try {
124 this.iStatus = Integer.parseInt(line.substring(prev + 1, rqt));
125 } catch (Exception e) {
126 throw new HttpException(HttpURLConnection.HTTP_BAD_METHOD, "Bad method");
127 }
128 this.iResponse = line.substring(rqt + 1);
129 return;
130 }
131 } else throw new HttpException(HttpURLConnection.HTTP_BAD_METHOD, "Bad method");
132 }
133 throw new HttpException(HttpURLConnection.HTTP_BAD_METHOD, "Bad method");
134 }
135
136
137
138
139
140
141 public int getMajorVersion() {
142 return this.iMajor;
143 }
144
145
146
147
148
149
150 public int getMinorVersion() {
151 return this.iMinor;
152 }
153
154
155
156
157
158
159 public String getMethodName() {
160 return this.iMethod;
161 }
162
163
164
165
166
167
168 public int getStatus() {
169 return this.iStatus;
170 }
171
172
173
174
175
176
177
178 public void write(ASCIIPrintStream pStream) {
179 pStream.print(this.iMethod + " " + this.iRequest + " HTTP/" + this.iMajor + "." + this.iMinor + "\r\n");
180 }
181
182
183
184
185
186
187 public String getResponseMessage() {
188 return this.iResponse;
189 }
190
191 @Override
192 public String toString() {
193 if (this.iIncomming) {
194 return this.iHttpHeader + " " + this.iStatus + " " + this.iResponse;
195 }
196 return this.iMethod + " " + this.iRequest + " HTTP/" + this.iMajor + "." + this.iMinor;
197 }
198 }