Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

*) httpdFileHandler.java: adding real streaming support for lage files - avoid to read the whole file into memory - support of chunked transfer-encoding for http/1.1 clients - support of gzip content-encoding suitable clients See: http://www.yacy-forum.de/viewtopic.php?p=17058#17058 *) MessageSend_p.html: better highlighting of peer response/status messages

theli62ffb5ece013
diff --git a/htroot/MessageSend_p.html b/htroot/MessageSend_p.html
index 4a2bb4396..401113ac1 100644
--- a/htroot/MessageSend_p.html
+++ b/htroot/MessageSend_p.html
@@ -19,7 +19,7 @@
::
<!-- we have the permission to send the message -->
<p>The peer <b>#[peerName]#</b> is alive and responded:</p>
- <p><tt>#[response]#
+ <p><tt class="MessageBackground">#[response]#
You are allowed to send me a message &le; #[messagesize]# kb and an
attachment &le; #[attachmentsize]#.</tt>
</p>
@@ -52,14 +52,14 @@
<!-- Message send successfully -->
<p>Your message has been sent. The target peer responded:</p>
- <p><tt>#[response]#</tt></p>
+ <p><tt class="MessageBackground">#[response]#</tt></p>
:: <!-- Message could not be send -->
<p>The target peer is alive but did not receive your message. Sorry.</p>
<p>
Here is a copy of your message, so you can copy it to save it for further attempts:<br>
- <tt>#[message]#</tt>
+ <tt class="MessageBackground">#[message]#</tt>
</p>
#(/status)#
diff --git a/source/de/anomic/http/httpd.java b/source/de/anomic/http/httpd.java
index 845c1716d..74d996076 100644
--- a/source/de/anomic/http/httpd.java
+++ b/source/de/anomic/http/httpd.java
@@ -1197,19 +1197,40 @@ public final class httpd implements serverHandler {
boolean nocache
) throws IOException {
- if(headers==null)headers = new httpHeader();
+ String reqMethod = conProp.getProperty(httpHeader.CONNECTION_PROP_METHOD);
+
+ if ((transferEnc != null) && !httpVersion.equals(httpHeader.HTTP_VERSION_1_1)) {
+ throw new IllegalArgumentException("Transfer encoding is only supported for http/1.1 connections.");
+ }
+ if (reqMethod.equals(httpHeader.METHOD_HEAD)) {
+ if (contentLength >= 0) {
+ throw new IllegalArgumentException("Http HEAD response messages MUST NOT contain a content-length.");
+ }
+ }
+ if (!reqMethod.equals(httpHeader.METHOD_HEAD)){
+ if (!conProp.getProperty(httpHeader.CONNECTION_PROP_PERSISTENT,"close").equals("close")) {
+ if (transferEnc == null && contentLength < 0) {
+ throw new IllegalArgumentException("Message MUST contain a Content-Length or a non-identity transfer-coding heder field.");
+ }
+ }
+ if (transferEnc != null && contentLength >= 0) {
+ throw new IllegalArgumentException("Messages MUST NOT include both a Content-Length header field and a non-identity transfer-coding.");
+ }
+ }
+
+ if(headers==null) headers = new httpHeader();
Date now = new Date(System.currentTimeMillis());
headers.put(httpHeader.SERVER, "AnomicHTTPD (www.anomic.de)");
headers.put(httpHeader.DATE, httpc.dateString(now));
- if (moddate.after(now)) moddate = now;
+ if (moddate.after(now)) moddate = now;
headers.put(httpHeader.LAST_MODIFIED, httpc.dateString(moddate));
if (nocache) {
if (httpVersion.toUpperCase().equals(httpHeader.HTTP_VERSION_1_1)) headers.put(httpHeader.CACHE_CONTROL, "no-cache");
else headers.put(httpHeader.PRAGMA, "no-cache");
}
- if (contentLength > 0) headers.put(httpHeader.CONTENT_TYPE, (contentType == null)? "text/html" : contentType);
+ headers.put(httpHeader.CONTENT_TYPE, (contentType == null)? "text/html" : contentType);
if (contentLength > 0) headers.put(httpHeader.CONTENT_LENGTH, Long.toString(contentLength));
//if (cookie != null) headers.put(httpHeader.SET_COOKIE, cookie);
if (expires != null) headers.put(httpHeader.EXPIRES, httpc.dateString(expires));
diff --git a/source/de/anomic/http/httpdFileHandler.java b/source/de/anomic/http/httpdFileHandler.java
index ed610f7ae..d7fac846f 100644
--- a/source/de/anomic/http/httpdFileHandler.java
+++ b/source/de/anomic/http/httpdFileHandler.java
@@ -661,23 +661,53 @@ public final class httpdFileHandler extends httpdAbstractHandler implements http
if (fis != null) try {fis.close(); fis=null;} catch(Exception e) {}
}
+ // write the array to the client
+ httpd.sendRespondHeader(this.connectionProperties, out, httpVersion, 200, null, mimeType, result.length, targetDate, null, tp.getOutgoingHeader(), (zipContent)?"gzip":null, null, nocache);
+ if (! method.equals(httpHeader.METHOD_HEAD)) {
+ Thread.sleep(200); // this solved the message problem (!!)
+ serverFileUtils.write(result, out);
+ }
+
} else { // no html
// write the file to the client
targetDate = new Date(targetFile.lastModified());
- result = (zipContent) ? serverFileUtils.readAndZip(targetFile) : serverFileUtils.read(targetFile);
+ long contentLength = method.equals(httpHeader.METHOD_HEAD)?-1:(zipContent)?-1:targetFile.length();
+ String contentEncoding = method.equals(httpHeader.METHOD_HEAD)?null:(zipContent)?"gzip":null;
+ String transferEncoding = (!httpVersion.equals(httpHeader.HTTP_VERSION_1_1))?null:(zipContent)?"chunked":null;
+ if (!httpVersion.equals(httpHeader.HTTP_VERSION_1_1) && zipContent) forceConnectionClose();
+
+ httpd.sendRespondHeader(this.connectionProperties, out, httpVersion, 200, null, mimeType, contentLength, targetDate, null, tp.getOutgoingHeader(), contentEncoding, transferEncoding, nocache);
+
+ if (!method.equals(httpHeader.METHOD_HEAD)) {
+ httpChunkedOutputStream chunkedOut = null;
+ GZIPOutputStream zipped = null;
+ OutputStream newOut = out;
+
+ if (transferEncoding != null) {
+ chunkedOut = new httpChunkedOutputStream(newOut);
+ newOut = chunkedOut;
+ }
+ if (contentEncoding != null) {
+ zipped = new GZIPOutputStream(newOut);
+ newOut = zipped;
+ }
+
+ serverFileUtils.copy(targetFile,newOut);
+
+ if (zipped != null) {
+ zipped.flush();
+ zipped.finish();
+ }
+ if (chunkedOut != null) {
+ chunkedOut.finish();
+ }
+ }
// check mime type again using the result array: these are 'magics'
// if (serverByteBuffer.equals(result, 1, "PNG".getBytes())) mimeType = mimeTable.getProperty("png","text/html");
// else if (serverByteBuffer.equals(result, 0, "GIF89".getBytes())) mimeType = mimeTable.getProperty("gif","text/html");
// else if (serverByteBuffer.equals(result, 6, "JFIF".getBytes())) mimeType = mimeTable.getProperty("jpg","text/html");
- //System.out.print("MAGIC:"); for (int i = 0; i < 10; i++) System.out.print(Integer.toHexString((int) result[i]) + ","); System.out.println();
- }
-
- // write the array to the client
- httpd.sendRespondHeader(this.connectionProperties, out, httpVersion, 200, null, mimeType, result.length, targetDate, null, tp.getOutgoingHeader(), (zipContent)?"gzip":null, null, nocache);
- if (! method.equals(httpHeader.METHOD_HEAD)) {
- Thread.sleep(200); // this solved the message problem (!!)
- serverFileUtils.write(result, out);
+ //System.out.print("MAGIC:"); for (int i = 0; i < 10; i++) System.out.print(Integer.toHexString((int) result[i]) + ","); System.out.println();
}
} else {
httpd.sendRespondError(conProp,out,3,404,"File not Found",null,null);