Skip to content
This repository has been archived by the owner on Jan 26, 2024. It is now read-only.

Commit

Permalink
Detect requests directly on "/" and send HTTP 303 redirect. Fixes iss…
Browse files Browse the repository at this point in the history
…ues with Reverse Proxy behind Apache2.
  • Loading branch information
Kline- committed Jan 21, 2012
1 parent 3c16b4d commit 7a9e706
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
11 changes: 11 additions & 0 deletions webserver/src/bmws.c
Expand Up @@ -42,6 +42,7 @@
static void sigHandler();
static int allowRemoteConnect, allowRemoteAdmin;
static int isLocalConnection(SOCKET socket);
struct serverInfo ServerInfo;

static void web(SOCKET fd){
// This gets run after we fork() for the client request
Expand Down Expand Up @@ -176,6 +177,16 @@ int main(){

readDbConfig();

// Save who we are
ServerInfo.port = port;
if( allowRemoteConnect ){
char hostname[SMALL_BUFSIZE];
gethostname(hostname,SMALL_BUFSIZE);
ServerInfo.hostname = strdup(hostname);
} else {
ServerInfo.hostname = strdup("localhost");
}

listener = setupListener();

while (1){
Expand Down
9 changes: 9 additions & 0 deletions webserver/src/bmws.h
Expand Up @@ -55,6 +55,12 @@
#define HEADER_CONTENT_TYPE "Content-Type"
#define HTTP_EOL "\r\n"

struct serverInfo{
char* hostname;
int port;
};
extern struct serverInfo ServerInfo;

struct HttpResponse{
int code;
char* msg;
Expand All @@ -65,6 +71,7 @@ struct NameValuePair{
char* value;
struct NameValuePair* next;
};

long getValueNumForName(char* name, struct NameValuePair* pair, long defaultValue);
char* getValueForName(char* name, struct NameValuePair* pair, char* defaultValue);
void freeNameValuePairs(struct NameValuePair* param);
Expand Down Expand Up @@ -113,6 +120,7 @@ void writeTextArrayToJson(SOCKET fd, char* key, char** values);
void writeNumValueToJson(SOCKET fd, char* key, BW_INT value);
void writeSyncData(SOCKET fd, struct Data* data);
void writeHeadersOk(SOCKET fd, char* contentType, int endHeaders);
void writeHeadersSeeOther(SOCKET fd, char* newPath, int endHeaders);
void writeHeadersNotFound(SOCKET fd, char* file);
void writeHeadersForbidden(SOCKET fd, char* request);
void writeHeadersNotAllowed(SOCKET fd, char* httpMethod);
Expand All @@ -121,6 +129,7 @@ void writeHeader(SOCKET fd, char* name, char* value);
void writeEndOfHeaders(SOCKET fd);
void processRequest(SOCKET fd, char* buffer, int allowAdmin);

static int getPort();
void getWebRoot(char* path);

#endif
10 changes: 9 additions & 1 deletion webserver/src/handleFile.c
Expand Up @@ -211,13 +211,16 @@ void doSubs(SOCKET fd, FILE* fp, struct NameValuePair* substPairs){
void processFileRequest(SOCKET fd, struct Request* req, struct NameValuePair* substPairs){
setupEnv();

int redirect = 0;
char* path = req->path;
// Default page is index.html, send this if no other file is specified
if (strcmp("/", path) == 0){
redirect = 1;
//free(req->path);
path = strdup("/index.html");

} else if ((strcmp("/m", path) == 0) || (strcmp("/m/", path) == 0)){
redirect = 1;
//free(req->path);
path = strdup("/m/index.xml");

Expand All @@ -244,7 +247,12 @@ void processFileRequest(SOCKET fd, struct Request* req, struct NameValuePair* su

} else {
// We got the file, write out the headers and then send the content
writeHeadersOk(fd, mimeType->contentType, TRUE);
if ( redirect ){
// Was the initial request only for "/" ?
writeHeadersSeeOther(fd, path, TRUE);
} else {
writeHeadersOk(fd, mimeType->contentType, TRUE);
}
if (substPairs == NULL){
int rc;
char buffer[BUFSIZE];
Expand Down
20 changes: 18 additions & 2 deletions webserver/src/http.c
Expand Up @@ -39,13 +39,16 @@ Contains HTTP-related functions, mostly for reading in requests and writing out
*/

static struct HttpResponse HTTP_OK = {200, "OK"};
static struct HttpResponse HTTP_SEE_OTHER = {303, "See Other"};
static struct HttpResponse HTTP_NOT_FOUND = {404, "Not Found"};
static struct HttpResponse HTTP_FORBIDDEN = {403, "Forbidden"};
static struct HttpResponse HTTP_NOT_ALLOWED = {405, "Method not allowed"};
static struct HttpResponse HTTP_SERVER_ERROR = {500, "Bad/missing parameter"};

static void writeHeaders(SOCKET fd, struct HttpResponse response, char* contentType, int endHeaders);


extern struct serverInfo ServerInfo;

// These are the different operations that we can perform on behalf of the client
enum OpType{File, Monitor, Summary, Query, Sync, Config, Alert, Export, RSS, MobileMonitor, MobileSummary, MobileAbout};

Expand Down Expand Up @@ -111,6 +114,10 @@ void writeHeadersOk(SOCKET fd, char* contentType, int endHeaders){
writeHeaders(fd, HTTP_OK, contentType, endHeaders);
}

void writeHeadersSeeOther(SOCKET fd, char* newPath, int endHeaders){
writeHeaders(fd, HTTP_SEE_OTHER, newPath, endHeaders);
}

static void writeHeaders(SOCKET fd, struct HttpResponse response, char* contentType, int endHeaders){
// Writes out a full set of headers including the specified HTTP response and, if appropriate, the MIME type
writeResponseCode(fd, response);
Expand All @@ -120,6 +127,14 @@ static void writeHeaders(SOCKET fd, struct HttpResponse response, char* contentT
writeMimeType(fd, contentType);
}

if (response.code == HTTP_SEE_OTHER.code && contentType != NULL){
char newLocation[SMALL_BUFSIZE];

sprintf(newLocation, "http://%s:%d%s", ServerInfo.hostname, ServerInfo.port, contentType);

writeHeader(fd, "Location", newLocation);
}

writeCommonHeaders(fd);

if (endHeaders){
Expand Down Expand Up @@ -174,7 +189,8 @@ void processRequest(SOCKET fd, char* buffer, int allowAdmin){
waitForMutex();
#endif

int needsDb = (op != File);
int needsDb = ((op != File) || (strcmp(req->path, "/") == 0));
// Special handling if someone only requested the index dir
if (needsDb){
// The client isn't asking for a file, so we will need a database connection to complete the request
openDb();
Expand Down

0 comments on commit 7a9e706

Please sign in to comment.