001: package com.quadcap.net.server;
002:
003: /* Copyright 1999 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.ByteArrayOutputStream;
042: import java.io.File;
043: import java.io.FileInputStream;
044: import java.io.InputStream;
045: import java.io.OutputStream;
046: import java.io.IOException;
047: import java.io.PrintStream;
048:
049: import java.net.*;
050:
051: import com.quadcap.io.IO;
052:
053: import com.quadcap.util.Debug;
054:
055: /**
056: * A simple worker for testing.
057: *
058: * @author Stan Bailes
059: */
060: public class SimpleWorker extends Worker {
061: byte[] response;
062: byte[] request;
063:
064: public void init(Server server, Object context) {
065: File f = new File(
066: "d:/butler/java/com/quadcap/http/server22a/index.html");
067: super .init(server, context);
068: ByteArrayOutputStream bos = new ByteArrayOutputStream();
069: PrintStream ps = new PrintStream(bos);
070: ps.println("Mime-Version: 1.0");
071: ps.println("Server: Quadcap Web Server 1.0dev");
072: ps.println("Content-Type: text/html");
073: ps.println("Content-Length: " + f.length());
074: ps.println("");
075: ps.flush();
076: try {
077: FileInputStream fis = new FileInputStream(f);
078: IO.copyStream(fis, bos);
079: } catch (IOException e) {
080: }
081:
082: response = bos.toByteArray();
083: request = new byte[4096];
084: }
085:
086: public void doSession() throws Exception {
087: int cnt = win.read(request, 0, 28);
088: wout.write(response);
089: }
090:
091: public static void main(String args[]) {
092: try {
093: SimpleWorker w = new SimpleWorker();
094: w.init(null, null);
095: final byte[] request = w.request;
096: final byte[] response = w.response;
097: final ServerSocket socket = new ServerSocket(80, 64);
098: while (true) {
099: Socket s = socket.accept();
100: InputStream in = s.getInputStream();
101: OutputStream out = s.getOutputStream();
102: in.read(request, 0, 28);
103: out.write(response);
104: s.close();
105: }
106: } catch (Throwable t) {
107: }
108: }
109: }
|