Why does a directory exist and not exist on a web server simultaneously? - http

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.

Related

Initial HTTP request: index or routing?

When we send an HTTP request to a web server as to load a web page e.g. http://wwww.nothing_is_here.com, what exactly the server does as to serve our request? Till now, I thought the server was looking to find a file named index (index.html, index.php) which should have HTML content and send it back to our browser. Now, I know this is not always the case. For example, in ASP .NET where we apply routing, home/index path is added to the URL by default as for our app to be routed. That's I cannot understand is how exactly the server acts upon a similar situation. Why it does not return an error message in case there is no index file, how it knows it has to apply routing rules? How can we instruct the server what to do in either case?
What the server does when it receives a request for the root (which is what it will receive in your example), is up to the server configuration. It is not within the control of the client making the request.
The server will be configured to have a default document (file name) in such cases, which is often index.html, but equally could be any file set by the administrator of the server.
The server will often be configured to recognise different hosts (e.g. if it's serving multiple sites on the same interface:port. These different sites will often have different configuration about what the default file name(s) is/are (if any). In some cases the server is configured to display a directory listing of a server-configured site root folder.

What status code to use when certifying an upload?

I'm working on a direct-to-S3 upload service that operates in two parts described below. This service would not be used by browsers, but would be a RESTful API used by other software clients.
Make a request to an endpoint which certifies and validates the upload, returning an upload URL if all's well.
Make a PUT request to the URL returned from #1 to actually do the upload to S3.
How should the server structure the response for the first endpoint?
The first option I am considering would be to use GET and return a status code 302 with a Content-Location header containing the URL to upload to. However, the intent behind the redirect descriptions in the spec seems to be focussed on redirecting after a form submission.
The other option I'm considering is to use POST for the first endpoint and returning a Location header with the URL, as described here:
If a resource has been created on the origin server, the response
SHOULD be 201 (Created) and contain an entity which describes the
status of the request and refers to the new resource, and a Location
header. RFC 2616 #9.5
Please advise on what other people have used in such circumstances?
I think it mainly depends on whether your API itself will have a resource referencing the uploaded file or not. The only one with knowledge of the uploaded file is the S3 itself or your API has something referencing it?
If the first case where only S3 knows about it, then it's OK to use the GET if it acts merely as a generator for the upload parameters, including the URI.
If the second case, then it shouldn't be a GET, since you're changing something on your side. Yes, you should make a POST, but the Location header should be used to return the URI for the created resource that references the uploaded file. That resource may have the upload URI and it could act like a state-machine, tracking if the file is uploaded or not. To avoid the need for clients to GET that resource before being able to upload, you may return the upload URI in the Link header, with a rel reflecting that purpose.

POST method WEBDAV not run

I had a server install WEBDAV and I try to do any WEBDAV method
all method like copy, delete, get, lock, mkcol, propfind, propatch run good.
I can create file and store data in server with PUT method. But I don't know how to do it in POST.
when I post anydata to exist file server return data of old file.
And I check in server this file not change any thing
when I try to post new file it return "The requested URL - URL of
file - was not found on this server."
when I try to post new file with out full path, I just send to
folder where I want to store file. I hope server create file with
auto gen Name of file and return name for me. But server return the
resource content in folder ( some thing like index.html )
I want to know how to implement POST method, it can create or repair file on WEBDAV server. any one can help me ?
In general, WebDAV servers do not support POST (more precisely: the WEBDAV specification doesn't mandate any specific behavior for POST, so servers vary in what they do).

passing http url as an get method variable - how to?

I am trying to do this:
http://somehost.net/edit.php?url=http://www.youtube.com/watch?v=EgHY53dOZ-U
Forbidden
You don't have permission to access edit.php on this server.
Is there a way to fix this through javascript(jquery), cause I am passing argument through ajax call.
I have tried it this way but without success:
$('#videofrm').load('edit.php?url='+encodeURI($(this).siblings('a').attr('href'))
You should fix the chmoding issues on the server.
Edit
What your edit.php doing ? If it redirecting to somewhere else ? then echo the result url before redirecting.
You can follow Tomalak Geret'kal if you want/can rewrite the .htaccess. otherwise you need to pass the url without the http:// part and prepend an http:// on edit.php
If you don't have permission to access edit.php, then it doesn't matter how many different ways you try to request it: you don't have permission.
Fix the permissions on the server, likely using chmod if the server is on Linux.
Update
You have a server configuration issue. I can only replicate the problem when passing the string :// inside the querystring.
Try writing AllowEncodedSlashes On in your httpd config, as per this question/answer.
You will then need to make sure you encode your URI properly:
http://somehost.net/edit.php?url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv=EgHY53dOZ-U
(it looks like your encodeURI call should take care of that part)
AllowEncodedSlashes allows slashes to be present in the query string as long as they're encoded which, for some reason, is not the case by default. The docs say that failure produces a 404, not a 403, but I still think that this is the cause.
If you are not able to manipulate configuration options for your webserver, workarounds include:
Choosing a stand-in term for http:// like http!!! that you will programmatically revert in the PHP script;
If you always use http:// (as opposed to, say, ftp:// or some local path), just leave it off the query string entirely and prepend it to the input in your PHP script (preferred workaround).
Hope that helps.

Get http url path of local file on server in asp.net

I'm using ASP.NET with MVC 2 and have trouble translating a local file url to a server address. It would seem like a fairly simple and common task, but google searches gives me no good answers. (Perhaps i suck at searching)
I have a controller that takes a file from a html form in a view and saves it to disk. I need to return the real url of this file back to the View. Whatever method i use, I always get a string with the local path of the file instead of the http path.
I suspect the url might get translated to http address once the project has been deployed, but I really need the server address when debugging without having to hardcode anything.
Consider the following example in some controller method:
string url = Url.RequestContext.HttpContext.Server.MapPath("~/Content/Files/" + Path.GetFileName(file.FileName));
// outputs: "C:\\Users\\xxx\\Documents\\Visual Studio 2010\\Projects\\[ProjectName]\\Content\\Files\\file.png"
// whereas i'd like something like "http://localhost/Content/Files/file.png" instead
any ideas?
It depends on where you are storing this file. If you are storing it in a directory which is outside of the virtual directory root you won't be able to access it. If the file is inside a folder which is accessible over HTTP you could simply use the Content method:
string url = Url.Content("~/Content/Files/foo.txt");

Resources