|
From: <bra...@us...> - 2009-10-28 01:04:46
|
Revision: 2857
http://archive-access.svn.sourceforge.net/archive-access/?rev=2857&view=rev
Author: bradtofel
Date: 2009-10-28 01:04:33 +0000 (Wed, 28 Oct 2009)
Log Message:
-----------
INITIAL REV: couldn't find anything out there that had the right interface: produce HTTP message and headers from a raw InputStream, so this is here until a better and usable package out there is discovered..
Added Paths:
-----------
trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/
trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/BadRequestException.java
trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpMessage.java
trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpRequest.java
trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpRequestMessage.java
trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpResponse.java
trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpResponseMessage.java
Added: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/BadRequestException.java
===================================================================
--- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/BadRequestException.java (rev 0)
+++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/BadRequestException.java 2009-10-28 01:04:33 UTC (rev 2857)
@@ -0,0 +1,34 @@
+/* BadRequestException
+ *
+ * $Id$
+ *
+ * Created on 3:56:12 PM Dec 16, 2008.
+ *
+ * Copyright (C) 2008 Internet Archive.
+ *
+ * This file is part of Wayback.
+ *
+ * SocksProxyCore is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * any later version.
+ *
+ * SocksProxyCore 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 Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser Public License
+ * along with SocksProxyCore; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package org.archive.wayback.util.http;
+
+import java.io.IOException;
+
+public class BadRequestException extends IOException {
+ private static final long serialVersionUID = -7123306169949959915L;
+ public BadRequestException(String message) {
+ super(message);
+ }
+}
Property changes on: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/BadRequestException.java
___________________________________________________________________
Added: svn:executable
+ *
Added: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpMessage.java
===================================================================
--- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpMessage.java (rev 0)
+++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpMessage.java 2009-10-28 01:04:33 UTC (rev 2857)
@@ -0,0 +1,165 @@
+/* HttpMessage
+ *
+ * $Id$
+ *
+ * Created on 5:48:40 PM Mar 2, 2009.
+ *
+ * Copyright (C) 2009 Internet Archive.
+ *
+ * This file is part of Wayback.
+ *
+ * ProxyServletCore is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * any later version.
+ *
+ * ProxyServletCore 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 Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser Public License
+ * along with ProxyServletCore; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package org.archive.wayback.util.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.archive.wayback.util.http.BadRequestException;
+import org.archive.wayback.util.ByteOp;
+
+/**
+ *
+ *
+ * @author brad
+ * @version $Date$, $Revision$
+ */
+
+public class HttpMessage {
+ private static int MAX_MESSAGE_SIZE = 4096;
+ public static byte SPACE = 32;
+ public static byte CR = 13;
+ public static byte LF = 10;
+
+ public static byte[] readLine(InputStream in, int max)
+ throws IOException, BadRequestException {
+
+ byte[] buffer = new byte[max];
+ int pos = 0;
+ boolean found = false;
+ while(pos < max) {
+ int next = in.read();
+ buffer[pos] = (byte) next;
+ if(next == LF) {
+ if(pos == 0) {
+ throw new BadRequestException(
+ "Message cannot start with LF");
+ }
+ if(buffer[pos - 1] == CR) {
+ found = true;
+ break;
+ }
+ }
+ pos++;
+ }
+ if(!found) {
+ throw new BadRequestException("Message too long without CRLF");
+ }
+ return ByteOp.copy(buffer,0,pos+1);
+ }
+
+ private static int[] findSpaces(byte[] buffer, int max)
+ throws BadRequestException {
+
+ int spaces[] = new int[max];
+ int found = 0;
+ int offset = 0;
+ int end = buffer.length - 2;
+ while(offset < end) {
+ if(buffer[offset] == SPACE) {
+ spaces[found] = offset;
+ found++;
+ }
+ if(found == max - 1) {
+ break;
+ }
+ offset++;
+ }
+ if(found != max - 1) {
+ throw new BadRequestException("Not enough fields(" + found +") " +
+ "want("+max+") in (" + new String(buffer)+ ")");
+ }
+ return spaces;
+ }
+
+ public static byte[][] loadFields(byte[] buffer, int max)
+ throws BadRequestException {
+
+ byte[][] fields = new byte[max][];
+ int[] offsets = findSpaces(buffer, max);
+ int start = 0;
+ for(int i = 0; i < max - 1; i++) {
+ fields[i] = ByteOp.copy(buffer, start, offsets[i] - start);
+ start = offsets[i] + 1;
+ }
+ fields[max-1] = ByteOp.copy(buffer, start, (buffer.length - 2) - start);
+ return fields;
+ }
+
+ public byte[] concatBytes(byte[][] fields, boolean addCrLf) {
+ int length = 1;
+ for(byte[] field : fields) {
+ length += field.length + 1;
+ }
+ if(!addCrLf) {
+ length -= 2;
+ }
+ byte[] buffer = new byte[length];
+ int index = 0;
+ for(byte[] field : fields) {
+ System.arraycopy(field, 0,
+ buffer, index, field.length);
+ index += field.length;
+ if(index < length) {
+ buffer[index] = SPACE;
+ }
+ index++;
+ }
+ if(addCrLf) {
+ buffer[length - 2] = CR;
+ buffer[length - 1] = LF;
+ }
+
+ return buffer;
+ }
+
+ public static HttpResponseMessage loadResponse(byte[] buffer)
+ throws BadRequestException {
+
+ byte[][] fields = loadFields(buffer,3);
+
+ return new HttpResponseMessage(fields[0],fields[1],fields[2]);
+ }
+
+ public static HttpResponseMessage loadResponse(InputStream in)
+ throws BadRequestException, IOException {
+
+ return loadResponse(readLine(in, MAX_MESSAGE_SIZE));
+ }
+
+ public static HttpRequestMessage loadRequest(byte[] buffer)
+ throws BadRequestException {
+
+ byte[][] fields = loadFields(buffer,3);
+
+ return new HttpRequestMessage(fields[0],fields[1],fields[2]);
+ }
+
+ public static HttpRequestMessage loadRequest(InputStream in)
+ throws BadRequestException, IOException {
+
+ return loadRequest(readLine(in, MAX_MESSAGE_SIZE));
+ }
+}
Added: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpRequest.java
===================================================================
--- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpRequest.java (rev 0)
+++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpRequest.java 2009-10-28 01:04:33 UTC (rev 2857)
@@ -0,0 +1,111 @@
+/* HttpRequest
+ *
+ * $Id$
+ *
+ * Created on 4:49:10 PM Dec 16, 2008.
+ *
+ * Copyright (C) 2008 Internet Archive.
+ *
+ * This file is part of Wayback.
+ *
+ * SocksProxyCore is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * any later version.
+ *
+ * SocksProxyCore 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 Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser Public License
+ * along with SocksProxyCore; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package org.archive.wayback.util.http;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.archive.util.anvl.ANVLRecord;
+import org.archive.wayback.util.http.BadRequestException;
+import org.archive.wayback.util.ByteOp;
+
+/**
+ *
+ *
+ * @author brad
+ * @version $Date$, $Revision$
+ */
+
+public class HttpRequest {
+
+ private static int MAX_HEADER_SIZE = 10240;
+
+ private HttpRequestMessage message = null;
+ private ANVLRecord headers = null;
+
+ private byte[] originalHeaders = null;
+
+ public HttpRequest(HttpRequestMessage message, byte[] originalHeaders)
+ throws IOException {
+
+ this.originalHeaders = originalHeaders;
+ this.message = message;
+ // If we want to keep the headers - we're not using them:
+ ByteArrayInputStream bais = new ByteArrayInputStream(originalHeaders);
+ headers = ANVLRecord.load(bais);
+ }
+
+ /**
+ * @return the headers
+ */
+ public ANVLRecord getHeaders() {
+ return headers;
+ }
+
+ /**
+ * @param headers the headers to set
+ */
+ public void setHeaders(ANVLRecord headers) {
+ this.headers = headers;
+ }
+
+ /**
+ * @return the inputBytes
+ */
+ public byte[] getOriginalHeaders() {
+ return originalHeaders;
+ }
+ public HttpRequestMessage getMessage() {
+ return message;
+ }
+ /**
+ * @return the method
+ */
+ public String getMethod() {
+ return message.getMethod();
+ }
+
+ /**
+ * @return the url
+ */
+ public String getPath() {
+ return message.getPath();
+ }
+
+ public static HttpRequest load(InputStream in)
+ throws IOException, BadRequestException {
+
+ HttpRequestMessage message = HttpMessage.loadRequest(in);
+
+ byte[] buffer = new byte[MAX_HEADER_SIZE];
+
+ int r = in.read(buffer, 0, MAX_HEADER_SIZE);
+ if(r == MAX_HEADER_SIZE) {
+ throw new BadRequestException("Request too long");
+ }
+ return new HttpRequest(message, ByteOp.copy(buffer,0,r));
+ }
+}
Added: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpRequestMessage.java
===================================================================
--- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpRequestMessage.java (rev 0)
+++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpRequestMessage.java 2009-10-28 01:04:33 UTC (rev 2857)
@@ -0,0 +1,151 @@
+/* HttpRequestMessage
+ *
+ * $Id$
+ *
+ * Created on 5:44:56 PM Mar 2, 2009.
+ *
+ * Copyright (C) 2009 Internet Archive.
+ *
+ * This file is part of Wayback.
+ *
+ * ProxyServletCore is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * any later version.
+ *
+ * ProxyServletCore 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 Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser Public License
+ * along with ProxyServletCore; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package org.archive.wayback.util.http;
+
+import org.archive.wayback.util.ByteOp;
+
+
+public class HttpRequestMessage extends HttpMessage {
+ private static byte[] METHOD_HEAD = {'H', 'E', 'A', 'D'};
+ private static byte[] METHOD_GET = {'G', 'E', 'T'};
+
+ private byte[] method = null;
+ private byte[] path = null;
+ private byte[] version = null;
+ public HttpRequestMessage(byte[] method, byte[] path, byte[] version) {
+ this.method = method;
+ this.path = path;
+ this.version = version;
+ }
+ public String getMethod() {
+ return new String(method);
+ }
+ public String getPath() {
+ return new String(path);
+ }
+ public String getVersion() {
+ return new String(version);
+ }
+ public HttpRequestMessage _clone() {
+ return new HttpRequestMessage(method,path,version);
+ }
+ public void setPath(byte[] path) {
+ this.path = path;
+ }
+ public byte[] getBytes(boolean addCrLf) {
+ byte[][] fields = {method,path,version};
+ return concatBytes(fields,addCrLf);
+ }
+
+// public byte[] getBytes() {
+// // ' ' + ' ' + \r\n = 4
+// int length = path.length + method.length + version.length + 4;
+// int versionStart = path.length + method.length + 2;
+//
+// byte[] buffer = new byte[length];
+//
+// System.arraycopy(method, 0,
+// buffer, 0, method.length);
+//
+// buffer[method.length] = SPACE;
+//
+// System.arraycopy(path, 0,
+// buffer, method.length + 1, path.length);
+//
+// buffer[versionStart - 1] = SPACE;
+//
+// System.arraycopy(version, 0,
+// buffer, versionStart, version.length);
+// buffer[versionStart + version.length] = CR;
+// buffer[versionStart + version.length + 1] = LF;
+//
+// return buffer;
+// }
+
+ public boolean isHead() {
+ return ByteOp.cmp(method,METHOD_HEAD);
+ }
+ public boolean isGet() {
+ return ByteOp.cmp(method,METHOD_GET);
+ }
+
+// public static HttpRequestMessage load(InputStream in)
+// throws BadRequestException, IOException {
+// return load(HttpMessage.readLine(in,MAX_SIZE));
+// }
+//
+// public static HttpRequestMessage load(byte[] buffer)
+// throws BadRequestException {
+//
+// byte[] method = null;
+// byte[] path = null;
+// byte[] version = null;
+//
+// int length = buffer.length;
+// int end = length - 2;
+// int firstSpace = 0;
+// int lastSpace = end;
+//
+//
+//
+// // make sure ends in CRLF:
+// if((buffer[length - 2] != CR)
+// || (buffer[length - 1] != LF)) {
+//
+// throw new BadRequestException("Bed end of Message(no CRLF): "
+// + new String(buffer));
+// }
+//
+// // find first ' ' (after METHOD):
+// while(firstSpace < end) {
+// if(buffer[firstSpace] == SPACE) {
+// method = ByteOp.copy(buffer, 0, firstSpace);
+// break;
+// }
+// firstSpace++;
+// }
+//
+// // find last ' ' (before VERSION):
+// while(lastSpace > firstSpace) {
+// if(buffer[lastSpace] == SPACE) {
+// version = ByteOp.copy(buffer, lastSpace + 1, end - (lastSpace+1));
+// break;
+// }
+// lastSpace--;
+// }
+// path = ByteOp.copy(buffer, firstSpace + 1, (lastSpace - firstSpace) - 1);
+// // make sure path has no spaces:
+// int position = 0;
+// while(position < path.length) {
+// if(path[position] == SPACE) {
+// throw new BadRequestException("Too many fields in Message: "
+// + new String(buffer));
+// }
+// position++;
+// }
+//// version = "HTTP/1.0".getBytes();
+// return new HttpRequestMessage(method, path, version);
+// }
+}
Property changes on: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpRequestMessage.java
___________________________________________________________________
Added: svn:executable
+ *
Added: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpResponse.java
===================================================================
--- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpResponse.java (rev 0)
+++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpResponse.java 2009-10-28 01:04:33 UTC (rev 2857)
@@ -0,0 +1,60 @@
+/* HttpResponse
+ *
+ * $Id$
+ *
+ * Created on 5:44:56 PM Mar 2, 2009.
+ *
+ * Copyright (C) 2009 Internet Archive.
+ *
+ * This file is part of Wayback.
+ *
+ * ProxyServletCore is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * any later version.
+ *
+ * ProxyServletCore 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 Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser Public License
+ * along with ProxyServletCore; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package org.archive.wayback.util.http;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.archive.util.anvl.ANVLRecord;
+import org.archive.wayback.util.http.BadRequestException;
+
+public class HttpResponse {
+ private HttpResponseMessage message = null;
+ private ANVLRecord headers = null;
+ private InputStream bodyInputStream = null;
+ public HttpResponse(HttpResponseMessage message, ANVLRecord headers,
+ InputStream bodyInputStream) {
+
+ this.message = message;
+ this.headers = headers;
+ this.bodyInputStream = bodyInputStream;
+ }
+ public HttpResponseMessage getMessage() {
+ return message;
+ }
+ public ANVLRecord getHeaders() {
+ return headers;
+ }
+ public InputStream getBodyInputStream() {
+ return bodyInputStream;
+ }
+ public static HttpResponse load(InputStream in)
+ throws BadRequestException, IOException {
+
+ HttpResponseMessage message = HttpMessage.loadResponse(in);
+ ANVLRecord headers = ANVLRecord.load(in);
+ return new HttpResponse(message,headers,in);
+ }
+}
Property changes on: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpResponse.java
___________________________________________________________________
Added: svn:executable
+ *
Added: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpResponseMessage.java
===================================================================
--- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpResponseMessage.java (rev 0)
+++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/util/http/HttpResponseMessage.java 2009-10-28 01:04:33 UTC (rev 2857)
@@ -0,0 +1,62 @@
+/* HttpResponseMessage
+ *
+ * $Id$
+ *
+ * Created on 5:44:56 PM Mar 2, 2009.
+ *
+ * Copyright (C) 2009 Internet Archive.
+ *
+ * This file is part of Wayback.
+ *
+ * ProxyServletCore is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * any later version.
+ *
+ * ProxyServletCore 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 Lesser Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser Public License
+ * along with ProxyServletCore; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+package org.archive.wayback.util.http;
+
+import org.archive.wayback.util.ByteOp;
+
+/**
+ *
+ *
+ * @author brad
+ * @version $Date$, $Revision$
+ */
+
+public class HttpResponseMessage extends HttpMessage {
+ private static byte[] HTTP_304 = {'3', '0', '4'};
+ private byte[] version = null;
+ private byte[] code = null;
+ private byte[] text = null;
+ public HttpResponseMessage(byte[] version, byte[] code, byte[] text) {
+ this.version = version;
+ this.code = code;
+ this.text = text;
+ }
+ public String getVersion() {
+ return new String(version);
+ }
+ public String getCode() {
+ return new String(code);
+ }
+ public String getText() {
+ return new String(text);
+ }
+ public boolean isNotModified() {
+ return ByteOp.cmp(code, HTTP_304);
+ }
+ public byte[] getBytes(boolean addCrLf) {
+ byte[][] fields = {version,code,text};
+ return concatBytes(fields, addCrLf);
+ }
+}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|