final Map<HTTPHeader,String> extraHeaders=new TreeMap<HTTPHeader, String>();
extraHeaders.put(HTTPHeader.CONTENT_TYPE, mimeType.getType());
confirmMimeType(request,mimeType);
HTTPStatus responseStatus = HTTPStatus.S200_OK;
DataBuffers buffers = null;
try
{
buffers = new CWDataBuffers(); // before forming output, process range request
switch(request.getMethod())
{
case HEAD:
case GET:
case POST:
{
final Class<? extends HTTPOutputConverter> converterClass=config.getConverters().findConverter(mimeType);
if(converterClass != null)
{
buffers=config.getFileCache().getFileData(pageFile, null);
//checkIfModifiedSince(request,buffers); this is RETARDED!!!
try
{
HTTPOutputConverter converter;
converter = converterClass.newInstance();
extraHeaders.put(HTTPHeader.CACHE_CONTROL, "no-cache");
final long dateTime=System.currentTimeMillis();
extraHeaders.put(HTTPHeader.EXPIRES, HTTPIOHandler.DATE_FORMAT.format(Long.valueOf(dateTime)));
buffers=new CWDataBuffers(converter.convertOutput(config, request, pathFile, HTTPStatus.S200_OK, buffers.flushToBuffer()), dateTime, true);
buffers = handleEncodingRequest(request, null, buffers, extraHeaders);
}
catch (final Exception e)
{
config.getLogger().throwing("", "", e);
throw HTTPException.standardException(HTTPStatus.S500_INTERNAL_ERROR);
}
}
else
{
final String[] eTagMarker =generateETagMarker(request);
buffers=config.getFileCache().getFileData(pageFile, eTagMarker);
if((eTagMarker[0]!=null)&&(eTagMarker[0].length()>0))
extraHeaders.put(HTTPHeader.ETAG, eTagMarker[0]);
checkIfModifiedSince(request,buffers);
buffers = handleEncodingRequest(request, pageFile, buffers, extraHeaders);
}
if(buffers == null)
{
throw HTTPException.standardException(HTTPStatus.S500_INTERNAL_ERROR);
}
final long fullSize = buffers.getLength();
final long[] fullRange = setRangeRequests(request, buffers);
if(fullRange != null)
{
responseStatus = HTTPStatus.S206_PARTIAL_CONTENT;
extraHeaders.put(HTTPHeader.CONTENT_RANGE, "bytes "+fullRange[0]+"-"+fullRange[1]+"/"+fullSize);
}
break;
}
default:
break;
}
lastHttpStatusCode=responseStatus.getStatusCode();
String specifiedHost = request.getHost(); // check for chunking
int chunkedSize = 0;
if(specifiedHost != null)
{
int portIndex=specifiedHost.indexOf(':');
if(portIndex > 0)
specifiedHost=specifiedHost.substring(0,portIndex);
final ChunkSpec chunkSpec = config.getChunkSpec(specifiedHost, request.getClientPort(), request.getUrlPath());
if((chunkSpec != null) && (buffers.getLength() >= chunkSpec.getMinFileSize()))
{
chunkedSize = chunkSpec.getChunkSize(); // set chunking flag
extraHeaders.put(HTTPHeader.TRANSFER_ENCODING, "chunked");
}
}
// finally, generate the response headers and body
switch(request.getMethod())
{
case HEAD:
final DataBuffers header=new CWDataBuffers(generateStandardHeaderResponse(request, responseStatus, extraHeaders, buffers), buffers.getLastModified().getTime(), false);
buffers.close();
return header;
case GET:
case POST:
final DataBuffers buf = generateStandardResponse(request, responseStatus, extraHeaders, buffers);
buf.setChunked(chunkedSize);
return buf;
default:
{
final HTTPException exception = new HTTPException(HTTPStatus.S405_METHOD_NOT_ALLOWED);
exception.getErrorHeaders().put(HTTPHeader.ALLOW, HTTPMethod.getAllowedList());