Skip to content

Commit

Permalink
*) introducing of additional constants
Browse files Browse the repository at this point in the history
   to improve maintainability of the sourcecode

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@113 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
theli committed May 13, 2005
1 parent e5e46ec commit aea3479
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 66 deletions.
72 changes: 72 additions & 0 deletions source/de/anomic/http/http.java
@@ -0,0 +1,72 @@
//http.java
//-----------------------
//(C) by Michael Peter Christen; mc@anomic.de
//first published on http://www.anomic.de
//Frankfurt, Germany, 2005
//last change: 13.05.2005 by Martin Thelian
//
//This program is free software; you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation; either version 2 of the License, or
//(at your option) any later version.
//
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//Using this software in any meaning (reading, learning, copying, compiling,
//running) means that you agree that the Author(s) is (are) not responsible
//for cost, loss of data or any harm that may be caused directly or indirectly
//by usage of this softare or this documentation. The usage of this software
//is on your own risk. The installation and usage (starting/running) of this
//software may allow other people or application to access your computer and
//any attached devices and is highly dependent on the configuration of the
//software which must be done by the user of the software; the author(s) is
//(are) also not responsible for proper configuration and usage of the
//software, even if provoked by documentation provided together with
//the software.
//
//Any changes to this file according to the GPL as documented in the file
//gpl.txt aside this file in the shipment you received can be done to the
//lines that follows this copyright notice here, but changes must not be
//done inside the copyright notive above. A re-distribution must contain
//the intact and unchanged copyright notice.
//Contributions and changes to the program code must be marked as such.

package de.anomic.http;

public final class http {

/* =============================================================
* Constants defining http methods
* ============================================================= */
public static final String METHOD_GET = "GET";
public static final String METHOD_HEAD = "HEAD";
public static final String METHOD_POST = "POST";
public static final String METHOD_CONNECT = "CONNECT";

/* =============================================================
* Constants defining http header names
* ============================================================= */
public static final String HEADER_ACCEPT = "Accept";
public static final String HEADER_ACCEPT_CHARSET = "Accept-Charset";
public static final String HEADER_ACCEPT_LANGUAGE = "Accept-Language";
public static final String HEADER_KEEP_ALIVE = "Keep-Alive";
public static final String HEADER_USER_AGENT = "User-Agent";
public static final String HEADER_HOST = "Host";
public static final String HEADER_CONNECTION = "Connection";
public static final String HEADER_REFERER = "Referer";
public static final String HEADER_ACCEPT_ENCODING = "Accept-Encoding";
public static final String HEADER_CONTENT_LENGTH = "CONTENT-LENGTH";
public static final String HEADER_CONTENT_TYPE = "CONTENT-TYPE";
public static final String HEADER_AUTHORIZATION = "Authorization";
public static final String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate";
public static final String HEADER_PROXY_AUTHORIZATION = "Proxy-Authorization";
public static final String HEADER_PROXY_AUTHENTICATE = "Proxy-Authenticate";

}
74 changes: 37 additions & 37 deletions source/de/anomic/http/httpc.java
Expand Up @@ -194,7 +194,7 @@ public static httpc getInstance(String server, int port, int timeout, boolean ss
newHttpc = (httpc) httpc.theHttpcPool.borrowObject();

} catch (Exception e) {
throw new IOException("Unable to initialize a new httpc. " + e.getMessage());
throw new IOException("Unable to fetch a new httpc from pool. " + e.getMessage());
}

// initialize it
Expand Down Expand Up @@ -586,67 +586,67 @@ private void send(String method, String path, httpHeader header, boolean zipped)
if (header == null) header = new httpHeader();

// set some standard values
if (!(header.containsKey("Accept")))
header.put("Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
if (!(header.containsKey("Accept-Charset")))
header.put("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
if (!(header.containsKey("Accept-Language")))
header.put("Accept-Language", "en-us,en;q=0.5");
if (!(header.containsKey("Keep-Alive")))
header.put("Keep-Alive", "300");
if (!(header.containsKey(http.HEADER_ACCEPT)))
header.put(http.HEADER_ACCEPT, "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
if (!(header.containsKey(http.HEADER_ACCEPT_CHARSET)))
header.put(http.HEADER_ACCEPT_CHARSET, "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
if (!(header.containsKey(http.HEADER_ACCEPT_LANGUAGE)))
header.put(http.HEADER_ACCEPT_LANGUAGE, "en-us,en;q=0.5");
if (!(header.containsKey(http.HEADER_KEEP_ALIVE)))
header.put(http.HEADER_KEEP_ALIVE, "300");

// set user agent. The user agent is only set if the value does not yet exists.
// this gives callers the opportunity, to change the user agent themselves, and
// it will not be changed.
if (!(header.containsKey("User-Agent"))) header.put("User-Agent", userAgent);
if (!(header.containsKey(http.HEADER_USER_AGENT))) header.put(http.HEADER_USER_AGENT, userAgent);

// set the host attribute. This is in particular necessary, if we contact another proxy
// the host is mandatory, if we use HTTP/1.1
if (!(header.containsKey("Host"))) {
if (!(header.containsKey(http.HEADER_HOST))) {
if (this.remoteProxyUse)
header.put("Host", savedRemoteHost);
header.put(http.HEADER_HOST, savedRemoteHost);
else
header.put("Host", this.host);
header.put(http.HEADER_HOST, this.host);
}

if (!(header.containsKey("Connection"))) {
header.put("Connection", "close");
if (!(header.containsKey(http.HEADER_CONNECTION))) {
header.put(http.HEADER_CONNECTION, "close");
}

// advertise a little bit...
if ((!(header.containsKey("Referer"))) || (((String) header.get("Referer")).trim().length() == 0)) {
header.put("Referer",
if ((!(header.containsKey(http.HEADER_REFERER))) || (((String) header.get(http.HEADER_REFERER)).trim().length() == 0)) {
header.put(http.HEADER_REFERER,
(((System.currentTimeMillis() >> 10) & 1) == 0) ?
"http://www.anomic.de" :
"http://www.yacy.net/yacy");
}

// stimulate zipping or not
// we can unzip, and we will return it always as unzipped, unless not wanted
if (header.containsKey("Accept-Encoding")) {
String encoding = (String) header.get("Accept-Encoding");
if (header.containsKey(http.HEADER_ACCEPT_ENCODING)) {
String encoding = (String) header.get(http.HEADER_ACCEPT_ENCODING);
if (zipped) {
if (encoding.indexOf("gzip") < 0) {
// add the gzip encoding
//System.out.println("!!! adding gzip encoding");
header.put("Accept-Encoding", "gzip,deflate" + ((encoding.length() == 0) ? "" : (";" + encoding)));
header.put(http.HEADER_ACCEPT_ENCODING, "gzip,deflate" + ((encoding.length() == 0) ? "" : (";" + encoding)));
}
} else {
int pos = encoding.indexOf("gzip");
if (pos >= 0) {
// remove the gzip encoding
//System.out.println("!!! removing gzip encoding");
header.put("Accept-Encoding", encoding.substring(0, pos) + encoding.substring(pos + 4));
header.put(http.HEADER_ACCEPT_ENCODING, encoding.substring(0, pos) + encoding.substring(pos + 4));
}
}
} else {
if (zipped) header.put("Accept-Encoding", "gzip,deflate");
if (zipped) header.put(http.HEADER_ACCEPT_ENCODING, "gzip,deflate");
}

//header = new httpHeader(); header.put("Host", this.host); // debug

// send request
if ((this.remoteProxyUse) && (!(method.equals("CONNECT"))))
if ((this.remoteProxyUse) && (!(method.equals(http.METHOD_CONNECT))))
path = "http://" + this.savedRemoteHost + path;
serverCore.send(clientOutput, method + " " + path + " HTTP/1.0"); // if set to HTTP/1.1, servers give time-outs?

Expand Down Expand Up @@ -687,7 +687,7 @@ public response GET(String path, httpHeader requestHeader) throws IOException {
//serverLog.logDebug("HTTPC", handle + " requested GET '" + path + "', time = " + (System.currentTimeMillis() - handle));
try {
boolean zipped = shallTransportZipped(path);
send("GET", path, requestHeader, zipped);
send(http.METHOD_GET, path, requestHeader, zipped);
response r = new response(zipped);
//serverLog.logDebug("HTTPC", handle + " returned GET '" + path + "', time = " + (System.currentTimeMillis() - handle));
return r;
Expand All @@ -698,7 +698,7 @@ public response GET(String path, httpHeader requestHeader) throws IOException {

public response HEAD(String path, httpHeader requestHeader) throws IOException {
try {
send("HEAD", path, requestHeader, false);
send(http.METHOD_HEAD, path, requestHeader, false);
return new response(false);
// in this case the caller should not read the response body,
// since there is none...
Expand All @@ -709,9 +709,9 @@ public response HEAD(String path, httpHeader requestHeader) throws IOException {

public response POST(String path, httpHeader requestHeader, InputStream ins) throws IOException {
try {
send("POST", path, requestHeader, false);
send(http.METHOD_POST, path, requestHeader, false);
// if there is a body to the call, we would have a CONTENT-LENGTH tag in the requestHeader
String cl = (String) requestHeader.get("CONTENT-LENGTH");
String cl = (String) requestHeader.get(http.HEADER_CONTENT_LENGTH);
int len, c;
byte[] buffer = new byte[512];
if (cl != null) {
Expand All @@ -727,7 +727,7 @@ public response POST(String path, httpHeader requestHeader, InputStream ins) thr
clientOutput.write(buffer, 0, c);
len += c;
}
requestHeader.put("CONTENT-LENGTH", "" + len);
requestHeader.put(http.HEADER_CONTENT_LENGTH, "" + len);
}
clientOutput.flush();
return new response(false);
Expand All @@ -738,7 +738,7 @@ public response POST(String path, httpHeader requestHeader, InputStream ins) thr

public response CONNECT(String host, int port, httpHeader requestHeader) throws IOException {
try {
send("CONNECT", host + ":" + port, requestHeader, false);
send(http.METHOD_CONNECT, host + ":" + port, requestHeader, false);
return new response(false);
} catch (SocketException e) {
throw new IOException(e.getMessage());
Expand All @@ -750,18 +750,18 @@ public response POST(String path, httpHeader requestHeader, serverObjects args,
// make shure, the header has a boundary information like
// CONTENT-TYPE=multipart/form-data; boundary=----------0xKhTmLbOuNdArY
if (requestHeader == null) requestHeader = new httpHeader();
String boundary = (String) requestHeader.get("CONTENT-TYPE");
String boundary = (String) requestHeader.get(http.HEADER_CONTENT_TYPE);
if (boundary == null) {
// create a boundary
boundary = "multipart/form-data; boundary=----------" + java.lang.System.currentTimeMillis();
requestHeader.put("CONTENT-TYPE", boundary);
requestHeader.put(http.HEADER_CONTENT_TYPE, boundary);
}
// extract the boundary string
int pos = boundary.toUpperCase().indexOf("BOUNDARY=");
if (pos < 0) {
// again, create a boundary
boundary = "multipart/form-data; boundary=----------" + java.lang.System.currentTimeMillis();
requestHeader.put("CONTENT-TYPE", boundary);
requestHeader.put(http.HEADER_CONTENT_TYPE, boundary);
pos = boundary.indexOf("boundary=");
}
boundary = "--" + boundary.substring(pos + "boundary=".length());
Expand Down Expand Up @@ -806,10 +806,10 @@ public response POST(String path, httpHeader requestHeader, serverObjects args,
byte[] body = buf.toByteArray();
//System.out.println("DEBUG: PUT BODY=" + new String(body));
// size of that body
requestHeader.put("CONTENT-LENGTH", "" + body.length);
requestHeader.put(http.HEADER_CONTENT_LENGTH, "" + body.length);
// send the header
//System.out.println("header=" + requestHeader);
send("POST", path, requestHeader, false);
send(http.METHOD_POST, path, requestHeader, false);
// send the body
//System.out.println("body=" + buf.toString());
serverCore.send(clientOutput, body);
Expand Down Expand Up @@ -877,7 +877,7 @@ public static byte[] singleGET(String host, int port, String path, int timeout,
httpHeader requestHeader) throws IOException {
if (requestHeader == null) requestHeader = new httpHeader();
if ((user != null) && (password != null) && (user.length() != 0)) {
requestHeader.put("Authorization", serverCodings.standardCoder.encodeBase64String(user + ":" + password));
requestHeader.put(http.HEADER_AUTHORIZATION, serverCodings.standardCoder.encodeBase64String(user + ":" + password));
}

httpc con = null;
Expand Down Expand Up @@ -935,7 +935,7 @@ public static byte[] singlePOST(String host, int port, String path, int timeout,

if (requestHeader == null) requestHeader = new httpHeader();
if ((user != null) && (password != null) && (user.length() != 0)) {
requestHeader.put("Authorization", serverCodings.standardCoder.encodeBase64String(user + ":" + password));
requestHeader.put(http.HEADER_AUTHORIZATION, serverCodings.standardCoder.encodeBase64String(user + ":" + password));
}

httpc con = null;
Expand Down Expand Up @@ -1003,7 +1003,7 @@ public static httpHeader whead(URL url, int timeout, String user, String passwor
// generate request header
httpHeader requestHeader = new httpHeader();
if ((user != null) && (password != null) && (user.length() != 0)) {
requestHeader.put("Authorization", serverCodings.standardCoder.encodeBase64String(user + ":" + password));
requestHeader.put(http.HEADER_AUTHORIZATION, serverCodings.standardCoder.encodeBase64String(user + ":" + password));
}
// parse query

Expand Down

0 comments on commit aea3479

Please sign in to comment.