| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.core; |
| 18 |
|
| 19 |
import android.util.Log; |
| 20 |
|
| 21 |
import java.io.*; |
| 22 |
import java.lang.Thread; |
| 23 |
import java.net.*; |
| 24 |
import java.util.*; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class TestWebServer implements HttpConstants { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
private final static String LOGTAG = "httpsv"; |
| 40 |
|
| 41 |
|
| 42 |
Vector threads = new Vector(); |
| 43 |
|
| 44 |
|
| 45 |
Vector activeThreads = new Vector(); |
| 46 |
|
| 47 |
|
| 48 |
int timeout = 0; |
| 49 |
|
| 50 |
|
| 51 |
int workers = 5; |
| 52 |
|
| 53 |
|
| 54 |
final static int DEFAULT_PORT = 8080; |
| 55 |
|
| 56 |
|
| 57 |
final static int DEFAULT_TIMEOUT = 5000; |
| 58 |
|
| 59 |
|
| 60 |
protected String HTTP_VERSION_STRING = "HTTP/1.1"; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private boolean http11 = true; |
| 66 |
|
| 67 |
|
| 68 |
private AcceptThread acceptT; |
| 69 |
|
| 70 |
|
| 71 |
int mTimeout; |
| 72 |
|
| 73 |
|
| 74 |
int mPort; |
| 75 |
|
| 76 |
|
| 77 |
boolean mLog = false; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
boolean keepAlive = true; |
| 83 |
|
| 84 |
|
| 85 |
boolean chunked = false; |
| 86 |
|
| 87 |
|
| 88 |
String redirectHost = null; |
| 89 |
|
| 90 |
|
| 91 |
int redirectCode = -1; |
| 92 |
|
| 93 |
|
| 94 |
int acceptLimit = 100; |
| 95 |
|
| 96 |
|
| 97 |
int acceptedConnections = 0; |
| 98 |
|
| 99 |
public TestWebServer() { |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public void initServer(boolean log) throws Exception { |
| 107 |
initServer(DEFAULT_PORT, DEFAULT_TIMEOUT, log); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public void initServer(int port, boolean log) throws Exception { |
| 116 |
initServer(port, DEFAULT_TIMEOUT, log); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public void initServer(int port, int timeout, boolean log) throws Exception { |
| 127 |
mPort = port; |
| 128 |
mTimeout = timeout; |
| 129 |
mLog = log; |
| 130 |
keepAlive = true; |
| 131 |
|
| 132 |
if (acceptT == null) { |
| 133 |
acceptT = new AcceptThread(); |
| 134 |
acceptT.init(); |
| 135 |
acceptT.start(); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
protected void log(String s) { |
| 144 |
if (mLog) { |
| 145 |
Log.d(LOGTAG, s); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public void setHttpVersion11(boolean set) { |
| 156 |
http11 = set; |
| 157 |
if (set) { |
| 158 |
HTTP_VERSION_STRING = "HTTP/1.1"; |
| 159 |
} else { |
| 160 |
HTTP_VERSION_STRING = "HTTP/1.0"; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public void setKeepAlive(boolean value) { |
| 170 |
keepAlive = value; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public void setChunked(boolean value) { |
| 179 |
chunked = value; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public void setAcceptLimit(int limit) { |
| 187 |
acceptLimit = limit; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public void setRedirect(String redirect, int code) { |
| 199 |
redirectHost = redirect; |
| 200 |
redirectCode = code; |
| 201 |
log("Server will redirect output to "+redirect+" code "+code); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
public void close() { |
| 208 |
|
| 209 |
if (acceptT != null) { |
| 210 |
log("Closing AcceptThread"+acceptT); |
| 211 |
acceptT.close(); |
| 212 |
acceptT = null; |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
class AcceptThread extends Thread { |
| 220 |
|
| 221 |
ServerSocket ss = null; |
| 222 |
boolean running = false; |
| 223 |
|
| 224 |
public void init() { |
| 225 |
|
| 226 |
InetSocketAddress ia = new InetSocketAddress(mPort); |
| 227 |
while (true) { |
| 228 |
try { |
| 229 |
ss = new ServerSocket(); |
| 230 |
|
| 231 |
|
| 232 |
ss.setReuseAddress(true); |
| 233 |
ss.bind(ia); |
| 234 |
break; |
| 235 |
} catch (IOException e) { |
| 236 |
log("IOException in AcceptThread.init()"); |
| 237 |
e.printStackTrace(); |
| 238 |
|
| 239 |
try { |
| 240 |
Thread.sleep(1000); |
| 241 |
} catch (InterruptedException e1) { |
| 242 |
|
| 243 |
e1.printStackTrace(); |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public synchronized void run() { |
| 253 |
running = true; |
| 254 |
try { |
| 255 |
while (running) { |
| 256 |
|
| 257 |
Socket s = ss.accept(); |
| 258 |
acceptedConnections++; |
| 259 |
if (acceptedConnections >= acceptLimit) { |
| 260 |
running = false; |
| 261 |
} |
| 262 |
|
| 263 |
Worker w = null; |
| 264 |
synchronized (threads) { |
| 265 |
if (threads.isEmpty()) { |
| 266 |
Worker ws = new Worker(); |
| 267 |
ws.setSocket(s); |
| 268 |
activeThreads.addElement(ws); |
| 269 |
(new Thread(ws, "additional worker")).start(); |
| 270 |
} else { |
| 271 |
w = (Worker) threads.elementAt(0); |
| 272 |
threads.removeElementAt(0); |
| 273 |
w.setSocket(s); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
} catch (SocketException e) { |
| 278 |
log("SocketException in AcceptThread: probably closed during accept"); |
| 279 |
running = false; |
| 280 |
} catch (IOException e) { |
| 281 |
log("IOException in AcceptThread"); |
| 282 |
e.printStackTrace(); |
| 283 |
running = false; |
| 284 |
} |
| 285 |
log("AcceptThread terminated" + this); |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
public void close() { |
| 290 |
try { |
| 291 |
running = false; |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
ss.close(); |
| 297 |
|
| 298 |
|
| 299 |
for (Enumeration e = activeThreads.elements(); e.hasMoreElements();) { |
| 300 |
Worker w = (Worker)e.nextElement(); |
| 301 |
w.close(); |
| 302 |
} |
| 303 |
activeThreads.clear(); |
| 304 |
|
| 305 |
} catch (IOException e) { |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
log("IOException caught by server socket close"); |
| 310 |
} |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
|
| 315 |
final static int BUF_SIZE = 2048; |
| 316 |
|
| 317 |
|
| 318 |
static final byte[] EOL = {(byte)'\r', (byte)'\n' }; |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
class Worker implements HttpConstants, Runnable { |
| 327 |
|
| 328 |
|
| 329 |
byte[] buf; |
| 330 |
|
| 331 |
|
| 332 |
private Socket s; |
| 333 |
|
| 334 |
|
| 335 |
private int requestMethod; |
| 336 |
|
| 337 |
|
| 338 |
private String testID; |
| 339 |
|
| 340 |
|
| 341 |
private int testNum; |
| 342 |
|
| 343 |
|
| 344 |
private boolean readStarted; |
| 345 |
|
| 346 |
|
| 347 |
private boolean hasContent = false; |
| 348 |
|
| 349 |
boolean running = false; |
| 350 |
|
| 351 |
|
| 352 |
private Hashtable<String, String> headers = new Hashtable<String, String>(); |
| 353 |
|
| 354 |
|
| 355 |
Worker() { |
| 356 |
buf = new byte[BUF_SIZE]; |
| 357 |
s = null; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
synchronized void setSocket(Socket s) { |
| 366 |
this.s = s; |
| 367 |
notify(); |
| 368 |
} |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
synchronized void close() { |
| 375 |
running = false; |
| 376 |
notify(); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
public synchronized void run() { |
| 385 |
running = true; |
| 386 |
while(running) { |
| 387 |
if (s == null) { |
| 388 |
|
| 389 |
try { |
| 390 |
log(this+" Moving to wait state"); |
| 391 |
wait(); |
| 392 |
} catch (InterruptedException e) { |
| 393 |
|
| 394 |
continue; |
| 395 |
} |
| 396 |
if (!running) break; |
| 397 |
} |
| 398 |
try { |
| 399 |
handleClient(); |
| 400 |
} catch (Exception e) { |
| 401 |
e.printStackTrace(); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
s = null; |
| 407 |
Vector pool = threads; |
| 408 |
synchronized (pool) { |
| 409 |
if (pool.size() >= workers) { |
| 410 |
|
| 411 |
activeThreads.remove(this); |
| 412 |
return; |
| 413 |
} else { |
| 414 |
pool.addElement(this); |
| 415 |
} |
| 416 |
} |
| 417 |
} |
| 418 |
log(this+" terminated"); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
private void clearBuffer() { |
| 425 |
for (int i = 0; i < BUF_SIZE; i++) { |
| 426 |
buf[i] = 0; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
private int readOneLine(InputStream is) { |
| 436 |
|
| 437 |
int read = 0; |
| 438 |
|
| 439 |
clearBuffer(); |
| 440 |
try { |
| 441 |
log("Reading one line: started ="+readStarted+" avail="+is.available()); |
| 442 |
while ((!readStarted) || (is.available() > 0)) { |
| 443 |
int data = is.read(); |
| 444 |
|
| 445 |
if (data == -1) { |
| 446 |
log("EOF returned"); |
| 447 |
return -1; |
| 448 |
} |
| 449 |
|
| 450 |
buf[read] = (byte)data; |
| 451 |
|
| 452 |
System.out.print((char)data); |
| 453 |
|
| 454 |
readStarted = true; |
| 455 |
if (buf[read++]==(byte)'\n') { |
| 456 |
System.out.println(); |
| 457 |
return read; |
| 458 |
} |
| 459 |
} |
| 460 |
} catch (IOException e) { |
| 461 |
log("IOException from readOneLine"); |
| 462 |
e.printStackTrace(); |
| 463 |
} |
| 464 |
return read; |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
private int readData(InputStream is, int length) { |
| 474 |
int read = 0; |
| 475 |
int count; |
| 476 |
|
| 477 |
byte[] buf = new byte[length]; |
| 478 |
|
| 479 |
try { |
| 480 |
while (is.available() > 0) { |
| 481 |
count = is.read(buf, read, length-read); |
| 482 |
read += count; |
| 483 |
} |
| 484 |
} catch (IOException e) { |
| 485 |
log("IOException from readData"); |
| 486 |
e.printStackTrace(); |
| 487 |
} |
| 488 |
return read; |
| 489 |
} |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
private int parseStatusLine(InputStream is) { |
| 498 |
int index; |
| 499 |
int nread = 0; |
| 500 |
|
| 501 |
log("Parse status line"); |
| 502 |
|
| 503 |
nread = readOneLine(is); |
| 504 |
|
| 505 |
if (nread == -1) { |
| 506 |
requestMethod = UNKNOWN_METHOD; |
| 507 |
return -1; |
| 508 |
} |
| 509 |
|
| 510 |
if (buf[0] == (byte)'G' && |
| 511 |
buf[1] == (byte)'E' && |
| 512 |
buf[2] == (byte)'T' && |
| 513 |
buf[3] == (byte)' ') { |
| 514 |
requestMethod = GET_METHOD; |
| 515 |
log("GET request"); |
| 516 |
index = 4; |
| 517 |
} else if (buf[0] == (byte)'H' && |
| 518 |
buf[1] == (byte)'E' && |
| 519 |
buf[2] == (byte)'A' && |
| 520 |
buf[3] == (byte)'D' && |
| 521 |
buf[4] == (byte)' ') { |
| 522 |
requestMethod = HEAD_METHOD; |
| 523 |
log("HEAD request"); |
| 524 |
index = 5; |
| 525 |
} else if (buf[0] == (byte)'P' && |
| 526 |
buf[1] == (byte)'O' && |
| 527 |
buf[2] == (byte)'S' && |
| 528 |
buf[3] == (byte)'T' && |
| 529 |
buf[4] == (byte)' ') { |
| 530 |
requestMethod = POST_METHOD; |
| 531 |
log("POST request"); |
| 532 |
index = 5; |
| 533 |
} else { |
| 534 |
|
| 535 |
requestMethod = UNKNOWN_METHOD; |
| 536 |
return -1; |
| 537 |
} |
| 538 |
|
| 539 |
|
| 540 |
if (requestMethod > UNKNOWN_METHOD) { |
| 541 |
|
| 542 |
int i = index; |
| 543 |
while (buf[i] != (byte)' ') { |
| 544 |
|
| 545 |
if ((buf[i] == (byte)'\n') || (buf[i] == (byte)'\r')) { |
| 546 |
requestMethod = UNKNOWN_METHOD; |
| 547 |
return -1; |
| 548 |
} |
| 549 |
i++; |
| 550 |
} |
| 551 |
|
| 552 |
testID = new String(buf, 0, index, i-index); |
| 553 |
if (testID.startsWith("/")) { |
| 554 |
testID = testID.substring(1); |
| 555 |
} |
| 556 |
|
| 557 |
return nread; |
| 558 |
} |
| 559 |
return -1; |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
private int parseHeader(InputStream is) { |
| 568 |
int index = 0; |
| 569 |
int nread = 0; |
| 570 |
log("Parse a header"); |
| 571 |
|
| 572 |
nread = readOneLine(is); |
| 573 |
|
| 574 |
if (nread == -1) { |
| 575 |
requestMethod = UNKNOWN_METHOD; |
| 576 |
return -1; |
| 577 |
} |
| 578 |
|
| 579 |
int i = index; |
| 580 |
while (buf[i] != (byte)':') { |
| 581 |
|
| 582 |
|
| 583 |
if ((buf[i] == (byte)'\n') || (buf[i] == (byte)'\r')) { |
| 584 |
return UNKNOWN_METHOD; |
| 585 |
} |
| 586 |
i++; |
| 587 |
} |
| 588 |
|
| 589 |
String headerName = new String(buf, 0, i); |
| 590 |
i++; |
| 591 |
while (buf[i] == ' ') { |
| 592 |
i++; |
| 593 |
} |
| 594 |
String headerValue = new String(buf, i, nread-1); |
| 595 |
|
| 596 |
headers.put(headerName, headerValue); |
| 597 |
return nread; |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
private int readHeaders(InputStream is) { |
| 606 |
int nread = 0; |
| 607 |
log("Read headers"); |
| 608 |
|
| 609 |
while (true) { |
| 610 |
int headerLen = 0; |
| 611 |
headerLen = parseHeader(is); |
| 612 |
if (headerLen == -1) |
| 613 |
return -1; |
| 614 |
nread += headerLen; |
| 615 |
if (headerLen <= 2) { |
| 616 |
return nread; |
| 617 |
} |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
private int readContent(InputStream is) { |
| 627 |
int nread = 0; |
| 628 |
log("Read content"); |
| 629 |
String lengthString = headers.get(requestHeaders[REQ_CONTENT_LENGTH]); |
| 630 |
int length = new Integer(lengthString).intValue(); |
| 631 |
|
| 632 |
|
| 633 |
length = readData(is, length); |
| 634 |
return length; |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
void handleClient() throws IOException { |
| 641 |
InputStream is = new BufferedInputStream(s.getInputStream()); |
| 642 |
PrintStream ps = new PrintStream(s.getOutputStream()); |
| 643 |
int nread = 0; |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
s.setSoTimeout(mTimeout); |
| 650 |
s.setTcpNoDelay(true); |
| 651 |
|
| 652 |
do { |
| 653 |
nread = parseStatusLine(is); |
| 654 |
if (requestMethod != UNKNOWN_METHOD) { |
| 655 |
|
| 656 |
|
| 657 |
nread = readHeaders(is); |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
if (headers.get(requestHeaders[REQ_CONTENT_LENGTH]) != null) { |
| 662 |
nread = readContent(is); |
| 663 |
} |
| 664 |
} else { |
| 665 |
if (nread > 0) { |
| 666 |
|
| 667 |
ps.print(HTTP_VERSION_STRING + " " + HTTP_BAD_METHOD + |
| 668 |
" unsupported method type: "); |
| 669 |
ps.write(buf, 0, 5); |
| 670 |
ps.write(EOL); |
| 671 |
ps.flush(); |
| 672 |
} else { |
| 673 |
} |
| 674 |
if (!keepAlive || nread <= 0) { |
| 675 |
headers.clear(); |
| 676 |
readStarted = false; |
| 677 |
|
| 678 |
log("SOCKET CLOSED"); |
| 679 |
s.close(); |
| 680 |
return; |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
testNum = -1; |
| 686 |
|
| 687 |
|
| 688 |
printStatus(ps); |
| 689 |
printHeaders(ps); |
| 690 |
|
| 691 |
|
| 692 |
psWriteEOL(ps); |
| 693 |
|
| 694 |
|
| 695 |
if (redirectCode == -1) { |
| 696 |
switch (requestMethod) { |
| 697 |
case GET_METHOD: |
| 698 |
if ((testNum < 0) || (testNum > TestWebData.tests.length - 1)) { |
| 699 |
send404(ps); |
| 700 |
} else { |
| 701 |
sendFile(ps); |
| 702 |
} |
| 703 |
break; |
| 704 |
case HEAD_METHOD: |
| 705 |
|
| 706 |
break; |
| 707 |
case POST_METHOD: |
| 708 |
|
| 709 |
if ((testNum > 0) || (testNum < TestWebData.tests.length - 1)) { |
| 710 |
sendFile(ps); |
| 711 |
} |
| 712 |
|
| 713 |
break; |
| 714 |
default: |
| 715 |
break; |
| 716 |
} |
| 717 |
} else { |
| 718 |
switch (redirectCode) { |
| 719 |
case 301: |
| 720 |
|
| 721 |
|
| 722 |
psPrint(ps, TestWebData.testServerResponse[TestWebData.REDIRECT_301]); |
| 723 |
break; |
| 724 |
case 302: |
| 725 |
|
| 726 |
psPrint(ps, TestWebData.testServerResponse[TestWebData.REDIRECT_302]); |
| 727 |
break; |
| 728 |
case 303: |
| 729 |
psPrint(ps, TestWebData.testServerResponse[TestWebData.REDIRECT_303]); |
| 730 |
break; |
| 731 |
case 307: |
| 732 |
psPrint(ps, TestWebData.testServerResponse[TestWebData.REDIRECT_307]); |
| 733 |
break; |
| 734 |
default: |
| 735 |
break; |
| 736 |
} |
| 737 |
} |
| 738 |
|
| 739 |
ps.flush(); |
| 740 |
|
| 741 |
|
| 742 |
readStarted = false; |
| 743 |
headers.clear(); |
| 744 |
|
| 745 |
} while (keepAlive); |
| 746 |
|
| 747 |
log("SOCKET CLOSED"); |
| 748 |
s.close(); |
| 749 |
} |
| 750 |
|
| 751 |
|
| 752 |
void psPrint(PrintStream ps, String s) throws IOException { |
| 753 |
log(s); |
| 754 |
ps.print(s); |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
void psWrite(PrintStream ps, byte[] bytes, int len) throws IOException { |
| 759 |
log(new String(bytes)); |
| 760 |
ps.write(bytes, 0, len); |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
void psWriteEOL(PrintStream ps) throws IOException { |
| 765 |
log("CRLF"); |
| 766 |
ps.write(EOL); |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
void printStatus(PrintStream ps) throws IOException { |
| 772 |
|
| 773 |
if (redirectCode != -1) { |
| 774 |
log("REDIRECTING TO "+redirectHost+" status "+redirectCode); |
| 775 |
psPrint(ps, HTTP_VERSION_STRING + " " + redirectCode +" Moved permanently"); |
| 776 |
psWriteEOL(ps); |
| 777 |
psPrint(ps, "Location: " + redirectHost); |
| 778 |
psWriteEOL(ps); |
| 779 |
return; |
| 780 |
} |
| 781 |
|
| 782 |
|
| 783 |
if (testID.startsWith("test")) { |
| 784 |
testNum = Integer.parseInt(testID.substring(4))-1; |
| 785 |
} |
| 786 |
|
| 787 |
if ((testNum < 0) || (testNum > TestWebData.tests.length - 1)) { |
| 788 |
psPrint(ps, HTTP_VERSION_STRING + " " + HTTP_NOT_FOUND + " not found"); |
| 789 |
psWriteEOL(ps); |
| 790 |
} else { |
| 791 |
psPrint(ps, HTTP_VERSION_STRING + " " + HTTP_OK+" OK"); |
| 792 |
psWriteEOL(ps); |
| 793 |
} |
| 794 |
|
| 795 |
log("Status sent"); |
| 796 |
} |
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
void printHeaders(PrintStream ps) throws IOException { |
| 802 |
psPrint(ps,"Server: TestWebServer"+mPort); |
| 803 |
psWriteEOL(ps); |
| 804 |
psPrint(ps, "Date: " + (new Date())); |
| 805 |
psWriteEOL(ps); |
| 806 |
psPrint(ps, "Connection: " + ((keepAlive) ? "Keep-Alive" : "Close")); |
| 807 |
psWriteEOL(ps); |
| 808 |
|
| 809 |
|
| 810 |
if (redirectCode == -1) { |
| 811 |
|
| 812 |
if (!TestWebData.testParams[testNum].testDir) { |
| 813 |
if (chunked) { |
| 814 |
psPrint(ps, "Transfer-Encoding: chunked"); |
| 815 |
} else { |
| 816 |
psPrint(ps, "Content-length: "+TestWebData.testParams[testNum].testLength); |
| 817 |
} |
| 818 |
psWriteEOL(ps); |
| 819 |
|
| 820 |
psPrint(ps,"Last Modified: " + (new |
| 821 |
Date(TestWebData.testParams[testNum].testLastModified))); |
| 822 |
psWriteEOL(ps); |
| 823 |
|
| 824 |
psPrint(ps, "Content-type: " + TestWebData.testParams[testNum].testType); |
| 825 |
psWriteEOL(ps); |
| 826 |
} else { |
| 827 |
psPrint(ps, "Content-type: text/html"); |
| 828 |
psWriteEOL(ps); |
| 829 |
} |
| 830 |
} else { |
| 831 |
|
| 832 |
psPrint(ps, "Content-length: "+(TestWebData.testServerResponse[TestWebData.REDIRECT_301]).length()); |
| 833 |
psWriteEOL(ps); |
| 834 |
psWriteEOL(ps); |
| 835 |
} |
| 836 |
log("Headers sent"); |
| 837 |
|
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
void send404(PrintStream ps) throws IOException { |
| 845 |
ps.println("Not Found\n\n"+ |
| 846 |
"The requested resource was not found.\n"); |
| 847 |
} |
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
void sendFile(PrintStream ps) throws IOException { |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
int dataSize = TestWebData.tests[testNum].length; |
| 858 |
if (chunked) { |
| 859 |
psPrint(ps, Integer.toHexString(dataSize)); |
| 860 |
psWriteEOL(ps); |
| 861 |
psWrite(ps, TestWebData.tests[testNum], dataSize); |
| 862 |
psWriteEOL(ps); |
| 863 |
psPrint(ps, "0"); |
| 864 |
psWriteEOL(ps); |
| 865 |
psWriteEOL(ps); |
| 866 |
} else { |
| 867 |
psWrite(ps, TestWebData.tests[testNum], dataSize); |
| 868 |
} |
| 869 |
} |
| 870 |
} |
| 871 |
} |