📄 httpframe.java
字号:
} return COND_OK; } } } else if (fresource != null) { long cmt = getLastModified(); if ( ims >= 0 ) { if (cmt > 0) { long s_cmt = cmt / 1000; long s_ims = ims / 1000; if (s_cmt < s_ims) { return COND_FAILED; } else if (s_cmt == s_ims) { return COND_WEAK; } return COND_OK; } } } return 0; } /** * Check the <code>If-Unmodified-Since</code> condition of that request. * @param request The request to check. * @return An integer, either <code>COND_FAILED</cond> if condition * was checked, but failed, <code>COND_OK</code> if condition was checked * and succeeded, or <strong>0</strong> if the condition was not checked * at all (eg because the resource or the request didn't support it). */ public int checkIfUnmodifiedSince(Request request) { if (fresource != null) { // Check for an If-Unmodified-Since conditional: long iums = request.getIfUnmodifiedSince(); long cmt = getLastModified(); if ( iums >= 0 ) return ((cmt > 0) && (cmt - 1000) >= iums) ? COND_FAILED : COND_OK; } return 0; } /** * Check the <code>Expect</code> condition of that request * @param request The request to check. * @return A boolean <code>true</code> if the requirement is known */ public boolean checkExpect(Request request) { // crude for now as we only support 100-continue // so FIXME for a more evolved version of this. String exp = request.getExpect(); if (exp != null) { if (!exp.equalsIgnoreCase(HTTP.HTTP_100_CONTINUE)) { return false; } } return true; } /** * check the validators namely LMT/Etags according to rfc2616 rules * @return An integer, either <code>COND_FAILED</cond> if condition * was checked, but failed, <code>COND_OK</code> if condition was checked * and succeeded, or <strong>0</strong> if the condition was not checked * at all (eg because the resource or the request didn't support it). */ public int checkValidators(Request request) { int v_inm = checkIfNoneMatch(request); int v_ims = checkIfModifiedSince(request); if ((v_inm == COND_OK) || (v_ims == COND_OK)) { return COND_OK; } if ((v_inm == COND_FAILED) || (v_ims == COND_FAILED)) { return COND_FAILED; } if ((v_inm == COND_WEAK) || (v_ims == COND_WEAK)) { return COND_FAILED; } return 0; } /** * Lookup the target resource. Lookup filters and then resource. * @param ls The current lookup state * @param lr The result * @return true if lookup is done. * @exception ProtocolException If an error relative to the protocol occurs * @see org.w3c.tools.resources.ResourceFrame#lookupFilters * @see #lookupResource */ public boolean lookup(LookupState ls, LookupResult lr) throws ProtocolException { RequestInterface req = ls.getRequest(); if (! checkRequest(req)) return false; if (lookupFilters(ls,lr)) return true; return lookupResource(ls,lr); } /** * Lookup the target resource (dispath to more specific lookup methods). * @param ls The current lookup state * @param lr The result * @return true if lookup is done. * @exception ProtocolException If an error relative to the protocol occurs * @see #lookupDirectory * @see #lookupFile * @see #lookupOther */ protected boolean lookupResource(LookupState ls, LookupResult lr) throws ProtocolException { if (fresource != null) { return lookupFile(ls,lr); } else if (dresource != null) { return lookupDirectory(ls,lr); } else { return lookupOther(ls,lr); } } /** * Lookup the target resource when associated with a DirectoryResource. * @param ls The current lookup state * @param lr The result * @return true if lookup is done. * @exception ProtocolException If an error relative to the protocol * occurs */ protected boolean lookupDirectory(LookupState ls, LookupResult lr) throws ProtocolException { // Give a chance to our super-class to run its own lookup scheme: // do we have to create a resource (PUT) ? if ((ls.hasMoreComponents()) && getPutableFlag()) { Request request = (Request) ls.getRequest() ; if ((request == null) || request.getMethod().equals("PUT")) { // We might well want to create a resource: String name = ls.peekNextComponent() ; ResourceReference rr = dresource.lookup(name); if ((rr == null) && (dresource.getExtensibleFlag())) { if (ls.countRemainingComponents() == 1) rr = dresource.createResource(name, request); else rr = dresource.createDirectoryResource(name); if (rr == null) { Reply error = request.makeReply(HTTP.UNSUPPORTED_MEDIA_TYPE); error.setContent( "Failed to create resource "+ name +" : "+ "Unable to create the appropriate file:"+ request.getURLPath()+ " this media type is not supported"); throw new HTTPException (error); } } else if (rr == null) { Reply error = request.makeReply(HTTP.FORBIDDEN) ; error.setContent("You are not allowed to create resource "+ name +" : "+ dresource.getIdentifier()+ " is not extensible."); throw new HTTPException (error); } } } if ( super.lookup(ls, lr) ) { if ( ! ls.isDirectory() && ! ls.isInternal() ) { // The directory lookup URL doesn't end with a slash: Request request = (Request)ls.getRequest() ; if ( request == null ) { lr.setTarget(null); return true; } URL url = null; try { if ((request != null ) && request.hasState(Request.ORIG_URL_STATE)) { URL oldurl; oldurl = (URL)request.getState(Request.ORIG_URL_STATE); url = new URL(oldurl, oldurl.getFile() + "/"); } else { url = (ls.hasRequest() ? getURL(request) : new URL(getServer().getURL(), resource.getURLPath())); } } catch (MalformedURLException ex) { getServer().errlog(this, "unable to build full URL."); throw new HTTPException("Internal server error"); } String msg = "Invalid requested URL: the directory resource "+ " you are trying to reach is available only through "+ " its full URL: <a href=\""+ url + "\">" + url + "</a>."; if ( getRelocateFlag() ) { // Emit an error (with reloc if allowed) Reply reloc = request.makeReply(HTTP.FOUND); reloc.setContent(msg) ; reloc.setLocation(url); lr.setTarget(null); lr.setReply(reloc); return true; } else { Reply error = request.makeReply(HTTP.NOT_FOUND) ; error.setContent(msg) ; lr.setTarget(null); lr.setReply(error); return true; } } else if ( ! ls.isInternal() ) { Request request = (Request)ls.getRequest() ; request.setState(STATE_CONTENT_LOCATION, "true"); // return the index file. String indexes[] = getIndexes(); if (indexes != null) { for (int i = 0 ; i < indexes.length ; i++) { String index = indexes[i]; if ( index != null && index.length() > 0) { DirectoryResource dir = (DirectoryResource) resource; ResourceReference rr = dir.lookup(index); if (rr != null) { try { FramedResource rindex = (FramedResource) rr.lock(); return rindex.lookup(ls,lr); } catch (InvalidResourceException ex) { } finally { rr.unlock(); } } } } } } return true; } return false; } /** * Lookup the target resource when associated with a FileResource. * @param ls The current lookup state * @param lr The result * @return true if lookup is done. * @exception ProtocolException If an error relative to the protocol occurs */ protected boolean lookupFile(LookupState ls, LookupResult lr) throws ProtocolException { return super.lookup(ls,lr); } /** * Lookup the target resource when associated with an unknown resource. * @param ls The current lookup state * @param lr The result * @return true if lookup is done. * @exception ProtocolException If an error relative to the protocol occurs */ protected boolean lookupOther(LookupState ls, LookupResult lr) throws ProtocolException { return super.lookup(ls,lr); } /** * Check the request. * @param request the incomming request. * @return true if the request is an HTTP Request. */ public boolean checkRequest(RequestInterface request) { return ((request == null) ? true :(request instanceof org.w3c.jigsaw.http.Request)); } /** * Perform the request on all the frames of that resource. The * Reply returned is the first non-null reply. * @param request A RequestInterface instance. * @return A ReplyInterface instance. * @exception ProtocolException If an error relative to the protocol occurs * @exception ResourceException If an error not relative to the * protocol occurs */ protected ReplyInterface performFrames(RequestInterface request) throws ProtocolException, ResourceException { return super.performFrames(request); } /** * Perform the request * @param req The request to handle. * @exception ProtocolException If processsing the request failed. * @exception ResourceException If the resource got a fatal error. */ public ReplyInterface perform(RequestInterface req) throws ProtocolException, ResourceException { ReplyInterface repi = super.perform(req); if (repi != null) return repi; if (! checkRequest(req)) return null; Reply reply = null; Request request = (Request) req; String method = request.getMethod () ; // Perform the request: if ( method.equals("GET") ) { reply = get(request) ; } else if ( method.equals("HEAD") ) { reply = head(request) ; } else if ( method.equals("POST") ) { reply = post(request) ; } else if ( method.equals("PUT") ) { reply = put(request) ; } else if ( method.equals("OPTIONS") ) { reply = options(request); } else if ( method.equals("DELETE") ) { reply = delete(request) ; } else if ( method.equals("LINK") ) { reply = link(request) ; } else if ( method.equals("UNLINK") ) { reply = unlink(request) ; } else if ( method.equals("TRACE") ) { reply = trace(request) ; } else { reply = extended(request) ; } return reply; } /** * The default GET 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 get(Request request) throws ProtocolException, ResourceException { if (dresource != null) { // we manage a DirectoryResource return getDirectoryResource(request) ; } else if (fresource != null) { // we manage a FileResource return getFileResource(request); } else { return getOtherResource(request); } } /** * The default GET method for other king of associated resource * @param request The request to handle. * @exception ProtocolException If processsing the request failed. * @exception ResourceException If the resource got a fatal error. */ protected Reply getOtherResource(Request request) throws ProtocolException, ResourceException { if (resource instanceof ContainerResource) { return getDirectoryResource(request); } else { // we don't manage this kind of resource Reply error = request.makeReply(HTTP.NOT_IMPLEMENTED) ; error.setContent("Method GET not implemented.") ; throw new HTTPException (error) ; } } /** * Create the reply relative to the given file. * @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 createFileReply(Request request) throws ProtocolException, ResourceException { File file = fresource.getFile() ; Reply reply = null; // Check for a range request: HttpRange ranges[] = request.getRange(); if ((ranges != null) && (ranges.length == 1)) { Reply rangereply = handleRangeRequest(request, ranges[0]); if ( rangereply != null ) return rangereply; } // Default to full reply: reply = createDefaultReply(request, HTTP.OK) ; try { reply.setStream(new FileInputStream(file)); } catch (IOException ex) { Reply error = request.makeReply(HTTP.SERVICE_UNAVAILABLE); error.setContent("Error while accessing filesystem"); return error; } return reply ; } /** * Alway throws an HTTPException */ protected Reply deleteMe(Request request) throws HTTPException { // Delete the resource if parent is extensible: boolean shrinkable = false; ResourceReference rr = fresource.getParent(); ResourceReference rrtemp = null; Resource p = null; while ( true ) { try { if (rr == null) break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -