httphandler.java

来自「21天学通java的示例程序源代码」· Java 代码 · 共 57 行

JAVA
57
字号
// HTTPHandler.java

package com.wrox.httpserver;

import java.io.*;
import java.net.*;

/**
 * Encapsulates an HTTP method handler such as GET, HEAD or POST.
 */
abstract class HTTPHandler extends HTTPConstants {

  // Instance variables
  protected HTTPResponse response;
  protected HTTPMessageHeaders headers;
  protected BufferedInputStream bis;
  protected BufferedOutputStream bos;
  protected String uri;
  protected HTTPInformation info;

  /**
   * Constructs an HTTPHandler. This constructor can only be called from the
   * respective method handler class.
   */
  public HTTPHandler(HTTPInformation info, String uri, 
                     HTTPMessageHeaders headers, InetAddress address, 
                     String protocol, BufferedInputStream bis, 
                     BufferedOutputStream bos) {

    // Initialize instance variables
    this.headers = headers;
    this.info = info;
    this.bis = bis;
    this.bos = bos;
    this.uri = uri;

    // Create an HTTP response
    response = new HTTPResponse();

    // Set up information
    info.serverProtocol = protocol;
    info.accept = headers.getHeader(HEADER_FIELD_ACCEPT);
    info.contentLength = headers.getHeader(HEADER_FIELD_CONTENTLENGTH);
    info.contentType = headers.getHeader(HEADER_FIELD_CONTENTTYPE);
    info.pragma = headers.getHeader(HEADER_FIELD_PRAGMA);
    info.referer = headers.getHeader(HEADER_FIELD_REFERER);
    info.userAgent = headers.getHeader(HEADER_FIELD_USERAGENT);
    info.remoteAddr = address.getHostAddress().toString();
    info.remoteHost = address.getHostName();
  }

  /**
   * Processes the corresponding method.
   */
  public abstract void process() throws HTTPException;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?