Howdy,
I have multiple header files and a source file. Each of the header files all #include each other (don't worry about why, you'll see below). The source file also #includes one of those headers.
This is a breakdown of the files:
connection.hpp - connection class
headermap.hpp - declaration of functions that take CONNECTION class as an argument, and are called BY the CONNECTION class.
stats.hpp - functions called by CONNECTION class and also take connection class as an argument.
There are a lot more headers included that use parts of the CONNECTION class, those are just the main ones.
So you can see that all three headers use parts of each other.
connection.cpp - function definitions of the connection class methods.
When I try to compile, I get errors on the headermap.hpp and stats.hpp files saying they don't know what CONNECTION is, but the class was already declared in connection.hpp (which they both #include).
All of the headers have the appropriate #ifndefs and #defines, so they should only be compiled once. I've posted the code below so you can see it properly if you wish. If anyone can tell me whats gone wrong I would really appreciate it.
NB- The code below will not compile anyway because theres a few other functions missing (I'm currently re-arranging everything).
SWEBSConnection.Hpp
-------------------------------------
Code:
#ifndef SWEBSCONNECTIONHPP
#define SWEBSCONNECTIONHPP 1
//--------------------------------------------------------------------------------
// SWEBSCONNECTION.hpp
// -------------------
// This is it. This is the most important part of the SWEBS source code,
// the code that handles all the requests. Everything the client will
// see comes through this class.
//
// When a request comes in, the main() function creates a new instance
// of the CONNECTION class, whos job it is to handle the connection. Main
// calls ReadRequest() to read in all the information from the client,
// and then calls HandleRequest(), which handles it.
//
// This is how the CONNECTION class is created:
// CONNECTION::CONNECTION(
// int SFD_SET, - Socket descriptor for this connection
// struct sockaddr_in, - Sockaddr_in structure used
// OPTIONS Options, - Options class, loaded in main
// STATS Stats); - Stats class, also loaded in main
//
//--------------------------------------------------------------------------------
// INCLUDES
//--------------------------------------------------------------------------------
#include "SWEBSStats.hpp"
#include "SWEBSHeaderMap.hpp"
#include "SWEBSCGI.hpp"
#include "SWEBSLog.hpp"
#include "SWEBSSocket.hpp"
#include "SWEBSUtilities.hpp"
#include "SWEBSOptions.hpp"
#include "SWEBSISAPI.hpp"
#include <windows.h>
#include <string>
#include <winsock.h>
#include <map>
#include <sstream>
#include <algorithm>
#include <fstream>
#include <stdio.h>
#include <ctime>
#include <httpext.h>
#include <httpfilt.h>
#include <header.h>
using namespace std;
//---------------------------------------------------------------------------------------------
// Connection class
//---------------------------------------------------------------------------------------------
class CONNECTION
{
public:
CONNECTION(int SFD_SET, struct sockaddr_in, OPTIONS Options, STATS Stats); // Constructor
CONNECTION();
bool LogConnection(); // Logs connection to the appropriate log
bool ReadRequest(); // Reads the request and sets values
bool HandleRequest(); // Handles the request
// Methods
bool SetFileType(); // Sets if the file is a script or binary
bool IndexFolder(); // Indexes the folder by listing all the files
bool SendText(); // Sends the requested file if it is text
bool SendCGI(); // Sends the requested file if it is a script
bool SendBinary(); // Sends the requested file if it is binary
bool SendError(); // Outputs the appropriate error code
bool SendISAPI(); // Gets the ISAPI request
string CalculateSize(); // Outputs the file size
bool ModifiedSince(string Date); // Was the file modifed since...
bool UnModifiedSince(string Date); // Is the file Unmodified since...
// Properties
int SFD; // Socket descriptor of connection
struct sockaddr_in ClientAddress; // Client address structure
OPTIONS Options; // Options file
STATS SWEBSStats; // Stats file
VIRTUALHOST *ThisHost; // This virtual host host
REQUEST_SPECIFIC_CGI CGIVariables; // CGI Environment variables
string FullRequest; // The entire input from the client
string RequestType; // Type of request (POST, GET etc)
string FileRequested; // String folling GET
string QueryString; // Anything after the '?' in the file
string Extension; // Extension of file requested
string RealFile; // Real path to file
string RealFileDate; // Last Modification date of RealFile
string HTTPVersion; // HTTP version of the client
istringstream PostData; // Data supplied AFTER the double newline, for post requests
string Headers; // Headers to be sent with the file
map <string, bool> Accepts; // MIME types the client accepts
string UserAgent; // Browser used by the user
string HostRequested; // Host: from browser
string From; // From: value (email address normally)
string ConnectionType; // Connection: type (keep alive normally)
string Referer; // Referer: file that refered the user to this document
string Date; // Date/time of this request
string ModifiedSinceStr; // IfModifiedSince string
string UnModifiedSinceStr; // IfUnModifiedSince string
bool UseModDate; // Do we use an If-Modified-Since
bool UseUnModDate; // Do we use the If-Unmodified-Since
int Status; // Status code for request (404, 200 etc)
bool UseVH; // Does the connection use a virtual host or a real one
bool IsFolder; // Is the file requested a folder or a file
bool IsBinary; // Is the file binary?
bool IsScript; // Is the file a script
bool IsIsapi; // The file requested uses ISAPI
bool IsAbsolute; // Did the client use an absolute address
};
//--------------------------------------------------------------------------------
extern map<int, CONNECTION *> Connections; // Map of all the connections
//--------------------------------------------------------------------------------
#endif
SWEBSHeaderMap.Hpp
--------------------------------
Code:
#ifndef SWEBSHEADERMAPHPP
#define SWEBSHEADERMAPHPP 1
//---------------------------------------------------------------------------------------------
/*
SWEBSHEADERMAP.HPP
--------------
This file contains functions used for header mapping. Header mapping
is the process of mapping a header such as "Host:" to a function to
handle that header. These functions change the Connection argument
depending on what headers are sent. Using a map its easy to disregard
headers we don't understand/support.
Header mapping can't be used until the function HeaderMapInit has been
called, which is usually done by the main server program.
The functions for this are in the library SWEBSHeadermap.lib
*/
//---------------------------------------------------------------------------------------------
#include "SWEBSLog.hpp"
#include "SWEBSStats.hpp"
#include "SWEBSCGI.hpp"
#include "SWEBSOptions.hpp"
#include "SWEBSConnection.hpp"
#include "SWEBSUtilities.hpp"
#include "SWEBSSocket.hpp"
#include "SWEBSISAPI.hpp"
#include <sstream>
using namespace std;
//---------------------------------------------------------------------------------------------
// Function Definitions
//---------------------------------------------------------------------------------------------
extern bool SWEBS_hm_ACCEPT_CHARSET(istringstream &IS, CONNECTION * Connection);
extern bool SWEBS_hm_ACCEPT_ENCODING(istringstream &IS, CONNECTION * Connection);
extern bool SWEBS_hm_ACCEPT_LANGUAGE(istringstream &IS, CONNECTION * Connection);
extern bool SWEBS_hm_AUTHORIZATION(istringstream &IS, CONNECTION * Connection);
extern bool SWEBS_hm_CONNECTION(istringstream &IS, CONNECTION * Connection);
extern bool SWEBS_hm_CONTENT_ENCODING(istringstream &IS, CONNECTION * Connection);
extern bool SWEBS_hm_CONTENT_LANGUAGE(istringstream &IS, CONNECTION * Connection);
extern bool SWEBS_hm_CONTENT_LENGTH(istringstream &IS, CONNECTION * Connection);
extern bool SWEBS_hm_CONTENT_TYPE(istringstream &IS, CONNECTION * Connection);
extern bool SWEBS_hm_FROM(istringstream &IS, CONNECTION * Connection);
extern bool SWEBS_hm_IF_MODIFIED_SINCE(istringstream &IS, CONNECTION * Connection);
extern bool SWEBS_hm_IF_NOT_MODIFIED_SINCE(istringstream &IS, CONNECTION * Connection);
extern bool SWEBS_hm_HOST(istringstream &IS, CONNECTION * Connection);
extern bool SWEBS_hm_REFERER(istringstream &IS, CONNECTION * Connection);
extern bool SWEBS_hm_USER_AGENT(istringstream &IS, CONNECTION * Connection);
typedef bool (*SWEBS_HM)(istringstream &IS, CONNECTION * Connection);
extern bool HeaderMapInit();
//---------------------------------------------------------------------------------------------
// MAP
//---------------------------------------------------------------------------------------------
extern HINSTANCE gSWEBS_headermapDLL;
extern map <string, SWEBS_HM>HeaderMap;
//---------------------------------------------------------------------------------------------
#endif
SWEBSStats.Hpp
------------------------
Code:
#ifndef SWEBSSTATS
#define SWEBSSTATS 1
//-------------------------------------------------------------------------------
// SWEBSSTATS.hpp
// -------------------
// This file contains functions and classes used when logging statistics.
//
//-------------------------------------------------------------------------------
// INCLUDES
//-------------------------------------------------------------------------------
#include <string>
#include <map>
#include "SWEBSLog.hpp"
#include "SWEBSHeaderMap.hpp"
#include "SWEBSCGI.hpp"
#include "SWEBSOptions.hpp"
#include "SWEBSConnection.hpp"
#include "SWEBSUtilities.hpp"
#include "SWEBSSocket.hpp"
#include "SWEBSISAPI.hpp"
using namespace std;
//--------------------------------------------------------------------------------
// VHSTATS Class
//--------------------------------------------------------------------------------
class VHSTATS
{
public:
VHSTATS();
VHSTATS(VIRTUALHOST); // Stats on a per-virtualhost basis
friend bool operator>(const VHSTATS lhs, const VHSTATS rhs); // Needed for use in map
friend bool operator<(const VHSTATS lhs, const VHSTATS rhs);
map <string, int> PageRequests; // Page requests (per VH)
unsigned long NumberOfRequests; // Number of requests
unsigned long BytesSent; // Bytes sent for this virtual host
VIRTUALHOST ThisVH; // This virtual host
};
//--------------------------------------------------------------------------------
// STATS Class
//--------------------------------------------------------------------------------
class STATS
{
public:
STATS(); // Constructor - must load the stats
bool WriteStatsFile(); // Writes all statistics to the stats file
unsigned long NumberOfRequests; // Number of connections served by the server
unsigned long TotalNumberOfRequests; // Total number of connections served
unsigned long BytesSent; // Total number of bytes served
unsigned long TotalBytesSent; // Total number of bytes sent for VH's and normal requests
string LastRestart; // Last time the server was restarted
map <string, int> PageRequests; // Page requests
map <VIRTUALHOST, VHSTATS> VirtualHosts; // To keep stats on a per-virtualhost basis
map <int, string> PageRequestIndex; // Index of pages requested
};
//--------------------------------------------------------------------------------
// Externals
//--------------------------------------------------------------------------------
extern bool CreateStatsThread(string File); // Creates the stats thread using FILE as the file to use
//--------------------------------------------------------------------------------
#endif
I won't post the whole of connection.cpp, just the top:
SWEBSConnection.Cpp
Code:
//--------------------------------------------------------------------------------
// INCLUDES
//--------------------------------------------------------------------------------
#include "../Include/SWEBSConnection.hpp"
#include <windows.h>
#include <string>
#include <winsock.h>
#include <map>
#include <sstream>
#include <algorithm>
#include <fstream>
#include <stdio.h>
#include <ctime>
#include <httpext.h>
#include <httpfilt.h>
#include <header.h>
using namespace std;
[...]
..define a bunch of functions
Can anyone think of why it is causing problems? I get errors saying no one knows what CONNECTION is.
BTW it's designed to compile as a .lib.
If anyone can help I would really appreciate it, thanks! :D