tags:

views:

100

answers:

5

I'm making an HTTP server in c++, I notice that the way apache works is if you request a directory without adding a forward slash at the end, firefox still somehow knows that it's a directory you are requesting (which seems impossible for firefox to do, which is why I'm assuming apache is doing a redirect).

Is that assumption right? Does apache check to see that you are requesting a directory and then does an http redirect to a request with the forward slash? If that is how apache works, how do I implement that in c++? Thanks to anyone who replies.

+1  A: 

If you wanted to do this, you would:

  1. call stat on the pathname
  2. determine that it is a directory
  3. send the necesssary HTTP response for a redirect

I'm not at all sure that you need to do this. Install the Firefox 'web developer' add-on to see exactly what goes back and forth.

bmargulies
+1  A: 

You should install Fiddler and observe the HTTP headers sent by other web servers.

Your question is impossible to answer precisely without more details, but you want to send an HTTP 3xx status code with a Location header.

SLaks
+1  A: 

Determine if the resource represents a directory, if so reply with a:

HTTP/1.X 301 Moved Permanently
Location: URI-including-trailing-slash

Using 301 allows user agents to cache the redirect.

Mic
Thank you, that is exactly what I did and it worked beautifully
Silmaril89
+1  A: 

Seriously, this should not be a problem. Suggestions for how to proceed:

  • Get the source code for Apache and look at what it does
  • Build a debug build of Apache and step through the code in a debugger in such a case; examine which pieces of code get run.
  • Install Wireshark (network analysis tool), Live HTTP Headers (Firefox extension) etc, and look at what's happening on the network
  • Read the relevant RFCs for HTTP - which presumably you should be keeping under your pillow anyway if you're writing a server.

Once you've done those things, it should be obvious how to do it. If you can't do those things, you should not be trying to develop a web server in C++.

MarkR
+1  A: 

The assumption is correct and make sure your response includes a Location header to the URL that allows directory listing and a legal 301/302 first line. It is not a C++ question, it is more of a HTTP protocol question, since you are trying to write a HTTP server, as one of the other posts suggests, read the RFC.

Murali VP