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 package org.apache.http.localserver;
29
30 import java.io.IOException;
31 import java.io.InputStream;
32 import java.io.OutputStream;
33 import java.util.Locale;
34
35 import org.apache.http.Consts;
36 import org.apache.http.HttpException;
37 import org.apache.http.HttpRequest;
38 import org.apache.http.HttpResponse;
39 import org.apache.http.HttpStatus;
40 import org.apache.http.MethodNotSupportedException;
41 import org.apache.http.entity.AbstractHttpEntity;
42 import org.apache.http.protocol.HttpContext;
43 import org.apache.http.protocol.HttpRequestHandler;
44
45
46
47
48 public class RandomHandler implements HttpRequestHandler {
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 @Override
66 public void handle(final HttpRequest request,
67 final HttpResponse response,
68 final HttpContext context)
69 throws HttpException, IOException {
70
71 final String method = request.getRequestLine().getMethod().toUpperCase(Locale.ROOT);
72 if (!"GET".equals(method) && !"HEAD".equals(method)) {
73 throw new MethodNotSupportedException
74 (method + " not supported by " + getClass().getName());
75 }
76
77 final String uri = request.getRequestLine().getUri();
78 final int slash = uri.lastIndexOf('/');
79 int length = -1;
80 if (slash < uri.length()-1) {
81 try {
82
83 length = Integer.parseInt(uri.substring(slash+1));
84
85 if (length < 0) {
86 response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
87 response.setReasonPhrase("LENGTH " + length);
88 }
89 } catch (final NumberFormatException nfx) {
90 response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
91 response.setReasonPhrase(nfx.toString());
92 }
93 } else {
94
95 length = 1 + (int)(Math.random() * 79.0);
96 }
97
98 if (length >= 0) {
99
100 response.setStatusCode(HttpStatus.SC_OK);
101
102 if (!"HEAD".equals(method)) {
103 final RandomEntity entity = new RandomEntity(length);
104 entity.setContentType("text/plain; charset=US-ASCII");
105 response.setEntity(entity);
106 } else {
107 response.setHeader("Content-Type",
108 "text/plain; charset=US-ASCII");
109 response.setHeader("Content-Length",
110 String.valueOf(length));
111 }
112 }
113
114 }
115
116
117
118
119
120
121
122 public static class RandomEntity extends AbstractHttpEntity {
123
124
125 private final static byte[] RANGE = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
126 .getBytes(Consts.ASCII);
127
128
129 protected final long length;
130
131
132
133
134
135
136
137
138 public RandomEntity(final long len) {
139 length = len;
140 }
141
142
143
144
145
146
147 @Override
148 public final boolean isStreaming() {
149 return false;
150 }
151
152
153
154
155
156
157
158
159 @Override
160 public boolean isRepeatable() {
161 return true;
162 }
163
164
165
166
167
168
169 @Override
170 public long getContentLength() {
171 return length;
172 }
173
174
175
176
177
178
179
180
181 @Override
182 public InputStream getContent() {
183 throw new UnsupportedOperationException();
184 }
185
186
187
188
189
190
191
192 @Override
193 public void writeTo(final OutputStream out) throws IOException {
194
195 final int blocksize = 2048;
196 int remaining = (int) length;
197 final byte[] data = new byte[Math.min(remaining, blocksize)];
198
199 while (remaining > 0) {
200 final int end = Math.min(remaining, data.length);
201
202 double value = 0.0;
203 for (int i = 0; i < end; i++) {
204
205 if (i%5 == 0) {
206 value = Math.random();
207 }
208 value = value * RANGE.length;
209 final int d = (int) value;
210 value = value - d;
211 data[i] = RANGE[d];
212 }
213 out.write(data, 0, end);
214 out.flush();
215
216 remaining = remaining - end;
217 }
218 out.close();
219
220 }
221
222 }
223
224
225 }