📄 httpframe.java
字号:
p = rr.lock(); if (p instanceof DirectoryResource) { shrinkable = ((DirectoryResource)p).getShrinkableFlag(); break; } rrtemp = p.getParent(); } catch (InvalidResourceException ex) { break; } finally { if (rr != null) rr.unlock(); } rr = rrtemp; } if (shrinkable) { // The resource is indexed but has no file, emit an error String msg = fresource.getFile()+ ": deleted, removing the FileResource."; getServer().errlog(fresource, msg); try { fresource.delete(); } catch (MultipleLockException ex) { Reply error = request.makeReply(HTTP.GONE) ; error.setContentMD5(null); // FIXME must compute it! error.setContent ("<h1>Document Gone</h1>"+ "<p>The document "+ request.getURL()+ " is indexed but no longer available.</p>"+ "<p>"+ex.getMessage()+"</p>"); throw new HTTPException (error) ; } } // Emit an error back: Reply error = request.makeReply(HTTP.GONE) ; error.setContentMD5(null); error.setContent ("<h1>Document Gone</h1>"+ "<p>The document "+ request.getURL()+ " is indexed but no longer available.</p>"); return error; } /** * Get for FileResource * @param request the incomming request. * @return A Reply instance * @exception ProtocolException If processsing the request failed. * @exception ResourceException If the resource got a fatal error. */ protected Reply getFileResource(Request request) throws ProtocolException, ResourceException { if (fresource == null) throw new ResourceException("this frame is not attached to a "+ "FileResource. ("+ resource.getIdentifier()+")"); Reply reply = null; if (!checkExpect(request)) { reply = createDefaultReply(request, HTTP.EXPECTATION_FAILED); reply.setContent("The requested expectation could not be"+ " met by the resource"); return reply; } File file = fresource.getFile() ; fresource.checkContent(); updateCachedHeaders(); // Check validators: int cim = checkIfMatch(request); if ((cim == COND_FAILED) || (cim == COND_WEAK)) { reply = request.makeReply(HTTP.PRECONDITION_FAILED); reply.setContent("Pre-conditions failed."); reply.setContentMD5(null); return reply; } if ( checkIfUnmodifiedSince(request) == COND_FAILED ) { reply = request.makeReply(HTTP.PRECONDITION_FAILED); reply.setContent("Pre-conditions failed."); reply.setContentMD5(null); return reply; } if (checkValidators(request) == COND_FAILED) { reply = createDefaultReply(request, HTTP.NOT_MODIFIED); return reply; } // Does this file really exists, if so send it back if ( file.exists() ) { reply = createFileReply(request); if (request.hasState(STATE_CONTENT_LOCATION)) reply.setContentLocation(getURL(request).toExternalForm()); return reply; } else { return deleteMe(request); } } /** * Perform a GET for the associated DirectoryResource. * @param request the incomming request. * @return A Reply instance. * @exception ProtocolException if request processing failed. * @exception ResourceException If the resource got a fatal error. */ protected Reply getDirectoryResource(Request request) throws ProtocolException, ResourceException { if (!checkExpect(request)) { Reply reply = createDefaultReply(request, HTTP.EXPECTATION_FAILED); reply.setContent("The requested expectation could not be"+ " met by the resource"); return reply; } String index = getIndex(); if ( index != null && index.length() > 0 ) { if (index.equals("*forbid*")) { Reply rep = request.makeReply(HTTP.FORBIDDEN); rep.setContent("<h1>Forbidden</h1>"+ "The directory resource "+ request.getURL() + " cannot be browsed"); return rep; } } return getDirectoryListing(request) ; } /** * The default HEAD method replies does a GET and removes entity. * @param request The request to handle. * @exception ProtocolException Always thrown, to return a NOT_IMPLEMENTED * error. * @exception ResourceException If the resource got a fatal error. */ public Reply head(Request request) throws ProtocolException, ResourceException { if (dresource != null) { return headDirectoryResource(request); } else if (fresource != null) { return headFileResource(request); } else { return headOtherResource(request); } } /** * Perform a HEAD request for the associated resource. * @param request the incomming request. * @return A Reply instance * @exception ProtocolException If processsing the request failed. * @exception ResourceException If the resource got a fatal error. */ protected Reply headOtherResource(Request request) throws ProtocolException, ResourceException { Reply reply = null; reply = getOtherResource(request) ; reply.setStream((InputStream) null); return reply; } /** * Perform a HEAD request for the associated DirectoryResource. * @param request the incomming request. * @return A Reply instance * @exception ProtocolException If processsing the request failed. * @exception ResourceException If the resource got a fatal error. */ protected Reply headDirectoryResource(Request request) throws ProtocolException, ResourceException { Reply reply = null; reply = getDirectoryResource(request) ; reply.setStream((InputStream) null); return reply; } /** * Perform a HEAD request for the associated FileResource. * @param request the incomming request. * @return A Reply instance * @exception ProtocolException If processsing the request failed. * @exception ResourceException If the resource got a fatal error. */ protected Reply headFileResource(Request request) throws ProtocolException, ResourceException { if (fresource == null) throw new ResourceException("this frame is not attached to a "+ "FileResource. ("+ resource.getIdentifier()+")"); Reply reply = null; if (!checkExpect(request)) { reply = createDefaultReply(request, HTTP.EXPECTATION_FAILED); reply.setContent("The requested expectation could not be"+ " met by the resource"); return reply; } fresource.checkContent(); updateCachedHeaders(); // Conditional check: int cim = checkIfMatch(request); if ((cim == COND_FAILED) || (cim == COND_WEAK)) { Reply r = request.makeReply(HTTP.PRECONDITION_FAILED); r.setContent("Pre-conditions failed."); return r; } if ( checkIfUnmodifiedSince(request) == COND_FAILED ) { Reply r = request.makeReply(HTTP.PRECONDITION_FAILED); r.setContent("Pre-conditions failed."); return r; } if ( checkValidators(request) == COND_FAILED) { return createDefaultReply(request, HTTP.NOT_MODIFIED); } if (! fresource.getFile().exists()) { return deleteMe(request); } reply = createDefaultReply(request, HTTP.OK); if (request.hasState(STATE_CONTENT_LOCATION)) reply.setContentLocation(getURL(request).toExternalForm()); return reply; } /** * The default POST method replies with a not implemented. * @param request The request to handle. * @exception ProtocolException Always thrown, to return a NOT_IMPLEMENTED * error. * @exception ResourceException If the resource got a fatal error. */ public Reply post(Request request) throws ProtocolException, ResourceException { if (!checkExpect(request)) { Reply reply = createDefaultReply(request, HTTP.EXPECTATION_FAILED); reply.setContent("The requested expectation could not be"+ " met by the resource"); return reply; } Reply error = request.makeReply(HTTP.NOT_ALLOWED) ; error.setHeaderValue(Reply.H_ALLOW, getAllow()); error.setContent("Method POST not allowed on this resource.") ; throw new HTTPException (error) ; } /** * The default PUT method. * @param request The request to handle. * @exception ProtocolException If processsing the request failed. * @exception ResourceException If the resource got a fatal error. */ public Reply put(Request request) throws ProtocolException, ResourceException { if (fresource != null) { return putFileResource(request); } else { return putOtherResource(request); } } /** * Always throw a ProtocolException. * @param request The incmming request. * @return a Reply instance. * @exception ProtocolException (Always thrown). */ protected Reply putOtherResource(Request request) throws ProtocolException { Reply error = request.makeReply(HTTP.NOT_IMPLEMENTED) ; error.setContent("Method PUT not implemented.") ; throw new HTTPException (error) ; } /** * Change the content of the associated FileResource. * @param request The incomming request. * @exception org.w3c.tools.resources.ProtocolException if a protocol * error occurs * @exception ResourceException If the resource got a fatal error. */ protected Reply putFileResource(Request request) throws ProtocolException, ResourceException { Reply reply = null; int status = HTTP.OK; if (!checkExpect(request)) { reply = createDefaultReply(request, HTTP.EXPECTATION_FAILED); reply.setContent("The requested expectation could not be"+ " met by the resource"); return reply; } fresource.checkContent(); updateCachedHeaders(); // Is this resource writable ? if ( ! getPutableFlag() ) { Reply error = request.makeReply(HTTP.NOT_ALLOWED) ; error.setHeaderValue(Reply.H_ALLOW, getAllow()); error.setContent("Method PUT not allowed.") ; throw new HTTPException (error) ; } // Check validators: int cim = checkIfMatch(request); if ((cim == COND_FAILED) || (cim == COND_WEAK) || (checkIfNoneMatch(request) == COND_FAILED) || (checkIfModifiedSince(request) == COND_FAILED) || (checkIfUnmodifiedSince(request) == COND_FAILED)) { Reply r = request.makeReply(HTTP.PRECONDITION_FAILED); r.setContent("Pre-condition failed."); return r; } // Check the request: InputStream in = null; try { in = request.getInputStream(); if ( in == null ) { Reply error = request.makeReply(HTTP.BAD_REQUEST) ; error.setContent ("<p>Request doesn't have a valid content."); throw new HTTPException (error) ; } } catch (IOException ex) { throw new ClientException(request.getClient(), ex); } // We do not support (for the time being) put with ranges: if ( request.hasContentRange() ) { Reply error = request.makeReply(HTTP.NOT_IMPLEMENTED); error.setContent("partial PUT not supported."); throw new HTTPException(error); }// REMOVED as it is impossile for clients to behave properly.// Of course it is fare more unsafe now, but it was too impractical IRL. // Check that if some type is provided it doesn't conflict:// if ( request.hasContentType() ) {// MimeType rtype = request.getContentType() ;// MimeType type = getContentType() ;// if ( type == null ) {// setValue (ATTR_CONTENT_TYPE, rtype) ;// } else if ( (rtype.match (type) < 0 ) && !rtype.equiv(type) ) {// if (debug) {// System.out.println("No match between: ["+// rtype.toString()+"] and ["+// type.toString()+"]");// }// Reply error = request.makeReply(HTTP.UNSUPPORTED_MEDIA_TYPE) ;// error.setContent ("<p>Invalid content type: "+rtype.toString()// + " is not matching resource MIME type: "// +type.toString());// throw new HTTPException (error) ;// }// } // Write the body back to the file: try { // We are about to accept the put, notify client before continuing Client client = request.getClient(); if ( client != null && request.getExpect() != null ) { // FIXME we should check for "100-continue" explicitely client.sendContinue(); } if ( fresource.newContent(request.getInputStream()) ) status = HTTP.CREATED; else status = HTTP.NO_CONTENT; } catch (IOException ex) { // so we have a problem replacing or creating the content // it is then a configuration problem (access right for the // underlying fle resource for example... Reply error = request.makeReply(HTTP.INTERNAL_SERVER_ERROR) ; error.setReason("File Access Error"); error.setContent("<p>Unable to save " + request.getURL() +" due to IO problems"); throw new HTTPException (error) ; } if ( status == HTTP.CREATED ) { reply = request.makeReply(status); reply.setContent("<P>Resource succesfully created"); if (request.hasState(STATE_CONTENT_LOCATION)) reply.setContentLocation(getURL(request).toExternalForm()); // Henrik's fix, create the Etag on 201 if (fresource != null) { // We only take car eof etag here: if ( etag == null ) { reply.setETag(getETag()); } } reply.setLocation(getURL(request)); reply.setContent ("<p>Entity body saved succesfully !") ; } else { reply = createDefaultReply(request, status); } return reply ; } /** * The default OPTIONS method replies with a not implemented. * @param request The request to handle. * @exception ProtocolException In case of errors. * @exception ResourceException If the resource got a fatal error. */ public Reply options(Request request) throws ProtocolException, ResourceException { if (!checkExpect(request)) { Reply reply = createDefaultReply(request, HTTP.EXPECTATION_FAILED); reply.setContent("The requested expectation could not be"+ " met by the resource"); return reply; } Reply reply = createDefaultReply(request, HTTP.OK); // set size to 0 according to rfc2616 9.2 reply.setContentLength(0); // Removed unused headers: reply.setContentType(null); // Add the allow header: reply.setHeaderValue(Reply.H_ALLOW, getAllow()); return reply; } /** * The default DELETE method, actually the resource (file, directory) * is moved into the trash directory which is not accessible
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -