Jetty has Resource and Contexts. I'm trying to understand them better.
Is a Resource a URL, a file path, or something else? Is Resource relative to the WAR, the filesystem its running on, or something else? How do you point a Resource to something inside the WAR?
What is a resourceBase? How does it affect resources?
And is Context just a way of saying the beginning of the URL path? If not, what is Context?
I've read the API docs but do not see these defined.
The word "Resource" is overloaded.
Resource is the term used in the HTTP spec when making a request to any URL (see "Target Resource")
Resource is the second letter in the acronym URL (Uniform Resource Locator) and URI (Uniform Resource Identifier).
Resource is also a definition for something that can be accessed by the server machine itself:
a filesystem path as a String (/path/to/my/...) reference to that path.
a filesystem path as a String URL (file://...) reference to that path.
a String URL (http://...) reference to non-filesystem resource.
a Classpath reference (/content/in/classpath)
a Jar File reference (jar:file:///path/to/foo.jar!/path/inside/)
a java.io.File referece.
a java.nio.file.Path reference.
a java.net.URL reference (can be any supported java url protocol/scheme).
a java.net.URI reference (can be any supported java url protocol/scheme).
A Resource can be a reference to a specific thing that has content (like a file) or a directory of other Resources.
A "Resource Base" is always a directory style reference.
A request for content within a "Resource Base" is always relative to that Resource Base and has protection to keep you in that Resource Base (or throw an error). (eg: Resource Base of /path/to/base/ and a request of /css/main.css will return /path/to/base/css/main.css, but requesting /../../../secret.txt will return a an error)
Now, the word "Context", this is what you see in the Servlet spec itself in the concept of javax.servlet.ServletContext
Defines a set of resources (physical, static, dynamic, generated, etc) that can be accessed via a common context path.
Processes (eg: servlets and filters) within that context can access the other resources within that context.
Processes outside of the context cannot access content within that context.
The Context itself holds information that all components within that context can access (eg: Attributes, init-parameters, security constraints, security roles, mime-type mappings, locale-encodings, session configuration, listeners, temp directories, work directories, etc..)
Related
While I'm sure the title could be improved for clarity, my meaning is thus:
When fetching a URL for a file download at http://example.com/dir1/dir2/file.zip, the response code is 200, yet attempting to access http://example.com/dir1, or http://example.com/dir1/dir2, elicits a 404 response code.
Why is this?
URLs don't necessarily correspond to actual directories on the server. Ultimately the path component of a URL is just a name; the server can translate that name to whatever it wants on the back end.
In this case it seems likely that /dir1/dir2/ is a directory on the server, but even so that doesn't mean anything. The server knows about a resource named /dir1/dir2/file.zip, but doesn't know anything about a resource named /dir1.
From https://en.wikipedia.org/wiki/Query_string
A web server can handle a Hypertext Transfer Protocol request either
by reading a file from its file system based on the URL path or by
handling the request using logic that is specific to the type of
resource. In cases where special logic is invoked, the query string
will be available to that logic for use in its processing, along with
the path component of the URL.
What does the quote mean by the two methods by which a web server can handle a HTTP request
"by reading a file from its file system based on the URL path"
"by handling the request using logic that is specific to the type of resource"?
Can you give specific examples to explain the two methods?
Is the query string used in both method?
Thanks.
by reading a file from its file system based on the URL path
^ The web site uses a generic mapping mechanism to convert a URL path to a local filesystem path, and then returns the file located at that path. This is common with static files like .css.
by handling the request using logic that is specific to the type of resource"
^ The web site turns control over to a web application, which contains code written by a developer. The code reads the query string and decides what to do. The logic for deciding what to do is completely customizable, and there does not need to be a static file in the local filesystem that matches the URL.
How would a HTTP Server differentiate whether the request in PUT is for a folder creation or a file creation in a directory.
For HTTP GET what I understood is, if the URL has a trailing /, then HTTP Server looks out for a folder with that name and, if does not exist can look out for a file.
How does this work for PUT for a new file and folder creation?
HTTP (the protocol) doesn't have any concept of files or folders. URIs are opaque, except when a relative URI reference is resolved against a base URI.
If you want your server to provide file/folder services, you may want to look into WebDAV (RFC 4918).
Assume that I have a http service which serves some contents, and I want to place cdn in front of it to serve cached content. the issue is, the url can take parameters, and mulitiple parameters maps to one result file, will cdn be efficient in this case? will cdn cache a different copy of the file for each of the strings that map to the same file?
For example:
http://myservice.com/getlogfile?time=10000
to
http://myservice.com/getlogfile?time=19999
all above maps to log.1
The Akamai cache configuration can be altered to adhere to specified rules. If you would like for one file to be cached for all query strings, simply set your Akamai configuration to ignore query strings. The Akamai Knowledge base found within the Akamai control panel contains a document called 'Edge Server Configuration Guide' that explains this configuration in detail. Start with the section called 'Ignore Query Strings' (page 104).
Consider this scenario:
You want to redirect (REDIRECT) a user to a certain handler (aspx or ashx) without hardcoded path.
You have the name of the handler's class, you can even get it's type (ASP.whateverpageclass).
Now, how do you get the virtual path?
If I understood correctly, you want a virtual path for a handler if you have the name/type of implementing class. AFAIK, this is not possible because there need not be one to one correlation between two - from ASP.NET run-time perspective, it has to map a virtual path to some handler class (and not a vice-verse). For example, you can have all paths to certain extension be mapped with the same handler type.
As far as your main problem of avoiding hard-coding is concerned, you can handled that having configurable url (to redirect) or have persistent store (database, xml file or config file) that can map some key (for example, handler class name) to virtual path to redirect.