*) 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

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@1646 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
theli 19 years ago
parent 3b6328ad02
commit 62ffb5ece0

@ -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)#

@ -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));

@ -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);

Loading…
Cancel
Save