Package sunlabs.brazil.util.http

Examples of sunlabs.brazil.util.http.HttpRequest

Otherwise, the HTTP request will go directly to the host:
header "Connection" or "Proxy-Connection"
The HttpRequest sets the appropriate connection header to "Keep-Alive" to keep alive the connection to the host or proxy (respectively). By setting the appropriate connection header, the user can control whether the HttpRequest tries to use Keep-Alives.
header "Host"
The HTTP/1.1 protocol requires that the "Host" header be set to the name of the machine being contacted. By default, this is derived from the URL used to construct the HttpRequest, and is set automatically if the user does not set it.
header "Content-Length"
If the user calls getOutputStream and writes some data to it, the "Content-Length" header will be set to the amount of data that has been written at the time that connect is called.
Once all data has been read from the remote host, the underlying socket may be automatically recycled and used again for subsequent requests to the same remote host. If the user is not planning on reading all the data from the remote host, the user should call close to release the socket. Although it happens under the covers, the user should be aware that if an IOException occurs or once data has been read normally from the remote host, close is called automatically. This is to ensure that the minimal number of sockets are left open at any time.

The input stream that getInputStream provides automatically hides whether the remote host is providing HTTP/1.1 "chunked" encoding or regular streaming data. The user can simply read until reaching the end of the input stream, which signifies that all the available data from this request has been read. If reading from a "chunked" source, the data is automatically de-chunked as it is presented to the user. Currently, no access is provided to the underlying raw input stream. @author Colin Stevens (colin.stevens@sun.com) @version 2.5


  if (!request.query.equals("")) {
      url += "?" + request.query;
  }

  HttpRequest target = new HttpRequest(requestPrefix + url);
  // System.out.println("Fetching: " + requestPrefix + url);
  if (proxyHost != null) {
      target.setProxy(proxyHost, proxyPort);
  }
  target.setMethod(request.method);

  HttpRequest.removePointToPointHeaders(request.headers, false);
        request.headers.remove("if-modified-since")// wrong spot XXX
  request.headers.copyTo(target.requestHeaders);

  /* XXX This doesn't belong here! - the proxy should do it */
  target.requestHeaders.remove("host");
  if (tokens != null) {
      target.addHeaders(tokens, request.props);
  }
  target.requestHeaders.putIfNotPresent("Host",host);

        boolean code=true;
  try {
      if (request.postData != null) {
    OutputStream out = target.getOutputStream();
    out.write(request.postData);
    out.close();
      }

      // Connect to target and read the response headers

      target.connect();
      // System.out.println("Got headers: " + target.responseHeaders);
      HttpRequest.removePointToPointHeaders(target.responseHeaders, true);
      target.responseHeaders.copyTo(request.responseHeaders);

      // Now filter the output, writing the header and content if true

      request.log(Server.LOG_DIAGNOSTIC,
      "  Response headers: " +  target.responseHeaders);
      if (shouldFilter(request.responseHeaders)) {
   
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    target.getInputStream().copyTo(out);

    request.log(Server.LOG_DIAGNOSTIC,
      "  parsing/modifying " + out.size() + " bytes");
    byte[] content = modifyContent(request, out.toByteArray());

    if (content == null) {  // This is wrong!!
        request.log(Server.LOG_DIAGNOSTIC,
          "  null content, returning false");
        code=false;
    } else {
        request.sendResponse(content, null);
    }
      } else {
    request.log(Server.LOG_DIAGNOSTIC, "Delivering normal content");
    request.sendResponse(target.getInputStream(),
      target.getContentLength(), null,
      target.getResponseCode());
      }
        } catch (InterruptedIOException e) {
            /*
             * Read timeout while reading from the remote side.  We use a
             * read timeout in case the target never responds.
             */
            request.sendError(408, "Timeout / No response");
        } catch (UnknownHostException e) {
            request.sendError(503, urlPrefix +  " Not reachable");
        } catch (ConnectException e) {
            request.sendError(500, "Connection refused");
  } catch (IOException e) {
      request.sendError(500, "Error retrieving response: " + e);
      e.printStackTrace();
        } finally {
            target.close();
      // System.out.println("Finally (proxy): " + code);
        }
  return code;
    }
View Full Code Here


      }
      href = sb.toString();
      debug(hr, "Using href: " + href);
  }

  HttpRequest target = new HttpRequest(href);
  clientHeaders.copyTo(target.requestHeaders);
  if (addheaders != null) {
      target.addHeaders(addheaders, hr.request.props);
  }
  target.requestHeaders.putIfNotPresent("Host",
      HttpUtil.extractUrlHost(href));
  target.setRequestHeader("Via", "Brazil-Include/2.0");

  if (proxyHost != null) {
      debug(hr, "Using proxy: " + proxy);
      target.setProxy(proxyHost, proxyPort);
  }
  hr.request.log(Server.LOG_INFORMATIONAL, hr.prefix,
    "Fetching: " + href);
  try {
      if (post != null) {
    OutputStream out = target.getOutputStream();
    out.write(post.getBytes());
    out.close();
      }
      String enc = target.getContent(encoding);
      if (name != null) {
    hr.request.props.put(hr.prefix + name, enc);
      } else {
    hr.append(enc);
      }
      if (getheaders != null) {
          Enumeration keys = target.responseHeaders.keys();
          while(keys.hasMoreElements()) {
        String key = (String) keys.nextElement();
        String full = hr.prefix + getheaders + "." + key;
        if (hr.request.props.containsKey(full)) {
            hr.request.props.put(full,
        hr.request.props.getProperty(full) +
              ", " + target.responseHeaders.get(key));
        } else {
            hr.request.props.put(full,
              target.responseHeaders.get(key));
        }
          }
    hr.request.props.put(hr.prefix + getheaders + ".status",
      target.status);
    String en = target.getEncoding();
    if (en != null) {
        hr.request.props.put(hr.prefix + getheaders +
          ".encoding", en);
    }
      }
View Full Code Here

      }
      href = sb.toString();
      debug(hr, "Using href: " + href);
  }

  HttpRequest target = new HttpRequest(href);
  clientHeaders.copyTo(target.requestHeaders);
  if (addheaders != null) {
      target.addHeaders(addheaders, hr.request.props);
  }
  target.requestHeaders.putIfNotPresent("Host",
      HttpUtil.extractUrlHost(href));
  target.setRequestHeader("Via", "Brazil-Include/2.0");

  if (proxyHost != null) {
      debug(hr, "Using proxy: " + proxy);
      target.setProxy(proxyHost, proxyPort);
  }
  hr.request.log(Server.LOG_INFORMATIONAL, hr.prefix,
    "Fetching: " + href);
  try {
      if (post != null) {
    OutputStream out = target.getOutputStream();
    out.write(post.getBytes());
    out.close();
      }
      String enc = target.getContent(encoding);
      if (name != null) {
    hr.request.props.put(hr.prefix + name, enc);
      } else {
    hr.append(enc);
      }
      if (getheaders != null) {
          Enumeration keys = target.responseHeaders.keys();
          while(keys.hasMoreElements()) {
        String key = (String) keys.nextElement();
        String full = hr.prefix + getheaders + "." + key;
        if (hr.request.props.containsKey(full)) {
            hr.request.props.put(full,
        hr.request.props.getProperty(full) +
              ", " + target.responseHeaders.get(key));
        } else {
            hr.request.props.put(full,
              target.responseHeaders.get(key));
        }
          }
    hr.request.props.put(hr.prefix + getheaders + ".status",
      target.status);
    String en = target.getEncoding();
    if (en != null) {
        hr.request.props.put(hr.prefix + getheaders +
          ".encoding", en);
    }
      }
View Full Code Here

      client.keepAlive = pc.equalsIgnoreCase("Keep-Alive");
  }

  HttpRequest.removePointToPointHeaders(clientHeaders, false);

        HttpRequest target = new HttpRequest(url);
  try {
      MimeHeaders targetHeaders = target.requestHeaders;

      target.setMethod(client.method);
      clientHeaders.copyTo(targetHeaders);
/*      targetHeaders.add("Via", client.protocol + via);*/
     
      /*
       * We might need to authenticate to a target proxy.
       */

      if ((proxyHost != null)
        && proxyTester.useProxy(target.host, target.port)) {
    target.setProxy(proxyHost, proxyPort);
    if (auth != null) {
        targetHeaders.add("Proxy-Authorization", auth);
    }
      }

      if (client.postData != null) {
    OutputStream out = target.getOutputStream();
    out.write(client.postData);
    out.close();
      }

      target.connect();

      targetHeaders = target.responseHeaders;
      HttpRequest.removePointToPointHeaders(targetHeaders, true);

      clientHeaders = client.responseHeaders;
      targetHeaders.copyTo(clientHeaders);
      try {
    clientHeaders.add("Via",
      target.status.substring(0, 8) + via);
      } catch (StringIndexOutOfBoundsException e) {
    clientHeaders.add("Via", via);
      }

      client.sendResponse(target.getInputStream(),
        target.getContentLength(), null, target.getResponseCode());
  } catch (InterruptedIOException e) {
      /*
       * Read timeout while reading from the remote side.  We use a
       * read timeout in case the target never responds. 
       */
      client.sendError(408, "Timeout / No response");
  } catch (EOFException e) {
      client.sendError(500, "No response");
  } catch (UnknownHostException e) {
      client.sendError(500, "Unknown host");
  } catch (ConnectException e) {
      client.sendError(500, "Connection refused");
  } catch (IOException e) {
      /*
       * An IOException will happen if we can't communicate with the
       * target or the client.  Rather than attempting to discriminate,
       * just send an error message to the client, and let the send
       * fail if the client was the one that was in error.
       */

      String msg = "Error from proxy";
      if (e.getMessage() != null) {
    msg += ": " + e.getMessage();
      }
      client.sendError(500, msg);
      } finally {
      target.close();
  }
  return true;
    }
View Full Code Here

      server.log(Server.LOG_DIAGNOSTIC, prefix, "Polling: " +  url);
      pollCount++;
      if (sessionTable == null) {
          props.clear();
      }
      HttpRequest target = new HttpRequest(url);
      if (proxyHost != null) {
    target.setProxy(proxyHost, proxyPort);
      }
      if (tokens != null) {
    target.addHeaders(tokens, server.props);
      }
      target.requestHeaders.putIfNotPresent("Host",
        HttpUtil.extractUrlHost(url));
      try {
    if (post != null) {
        OutputStream out = target.getOutputStream();
        String data = Format.subst(server.props, post);
        out.write(data.getBytes()); // XXX choose encoding here
        out.close();
    }
    target.connect();
    int code = target.getResponseCode();
    if (code == 200) {
        fillProps(props, target);
    } else {
        throw new Exception("Request failed, code: " + code);
    }
      } catch (Exception e) {
          System.out.println("Oops in fillProps: " + e);
          // e.printStackTrace();
          errorCount++;
          props.put("error.msg", e.getMessage());
          props.put("error.time", "" + System.currentTimeMillis());
          props.put("error.at", "" + pollCount);
      } finally {
          target.close();
      }
      server.log(Server.LOG_DIAGNOSTIC, prefix, " found " + props.size() +
    " items");
      props.put("timestamp", "" + System.currentTimeMillis());
      props.put("count.attempts", "" + pollCount);
View Full Code Here

TOP

Related Classes of sunlabs.brazil.util.http.HttpRequest

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.