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
27
28
29 package org.metricshub.wbem.sblim.cimclient.internal.http;
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 import java.io.IOException;
52 import java.io.InputStream;
53 import java.util.logging.Level;
54 import org.metricshub.wbem.sblim.cimclient.internal.http.io.BoundedInputStream;
55 import org.metricshub.wbem.sblim.cimclient.internal.http.io.ChunkedInputStream;
56 import org.metricshub.wbem.sblim.cimclient.internal.http.io.PersistentInputStream;
57 import org.metricshub.wbem.sblim.cimclient.internal.logging.LogAndTraceBroker;
58
59
60
61
62
63 public class MessageReader {
64 HttpHeader iHeader;
65
66 HttpServerMethod iMethod;
67
68 private boolean iChunked = false;
69
70 private String iEncoding = "UTF-8";
71
72 InputStream iContent;
73
74
75
76
77
78
79
80
81
82
83
84 public MessageReader(InputStream pStream, int pTimeout) throws IOException, HttpException {
85 this.iMethod = new HttpServerMethod(pStream);
86 this.iHeader = new HttpHeader(pStream, pTimeout);
87
88 String encoding = this.iHeader.getField("Transfer-Encoding");
89 if ((encoding != null) && (encoding.toLowerCase().endsWith("chunked"))) {
90 this.iChunked = true;
91 }
92 String length = this.iHeader.getField("Content-Length");
93 int contentLength = -1;
94 if (length != null && length.length() > 0) {
95 try {
96 contentLength = Integer.parseInt(length);
97 } catch (Exception e) {
98 LogAndTraceBroker.getBroker().trace(Level.FINER, "Exception while parsing http content-length", e);
99 }
100 }
101 String contentType = this.iHeader.getField("Content-Type");
102 if (contentType != null) {
103 try {
104 HttpHeaderParser contentTypeHeader = new HttpHeaderParser(contentType);
105 encoding = contentTypeHeader.getValue("charset");
106 } catch (Exception e) {
107 encoding = "UTF-8";
108
109 LogAndTraceBroker.getBroker().trace(Level.FINER, "Exception while parsing http content-type", e);
110 }
111 this.iEncoding = encoding;
112 }
113
114 this.iContent = new PersistentInputStream(pStream, isPersistentConnectionSupported());
115 if (this.iChunked) {
116 this.iContent = new ChunkedInputStream(this.iContent, this.iHeader.getField("Trailer"), "Indication Request");
117 } else if (contentLength >= 0) {
118 this.iContent = new BoundedInputStream(this.iContent, contentLength);
119 }
120 }
121
122
123
124
125
126
127 public String getCharacterEncoding() {
128 return this.iEncoding;
129 }
130
131
132
133
134
135
136 public HttpHeader getHeader() {
137 return this.iHeader;
138 }
139
140
141
142
143
144
145 public HttpServerMethod getMethod() {
146 return this.iMethod;
147 }
148
149
150
151
152
153
154 public InputStream getInputStream() {
155 return this.iContent;
156 }
157
158
159
160
161
162
163 public boolean isPersistentConnectionSupported() {
164 String conn = this.iHeader.getField("Connection");
165 if (conn != null) {
166 if (conn.equalsIgnoreCase("close")) return false;
167 if (conn.equalsIgnoreCase("Keep-Alive")) return true;
168 }
169
170 return ((this.iMethod.getMajorVersion() >= 1) && (this.iMethod.getMinorVersion() >= 1));
171 }
172
173
174
175
176
177
178 public boolean isChunkSupported() {
179
180 if ((this.iMethod.getMajorVersion() >= 1) && (this.iMethod.getMinorVersion() >= 1)) {
181 String TE;
182 if ((TE = this.iHeader.getField("TE")) != null && (TE.equalsIgnoreCase("trailers"))) {
183 return true;
184 }
185 }
186 return false;
187 }
188
189
190
191
192
193
194 public void close() throws IOException {
195 this.iContent.close();
196 }
197 }