This contains a simple multithreaded TCP server manager. It sits around
[the Webserver part]


Defines

#define DEF_MEM_LEVEL   8
 uses the same calling syntax as compress2(), but it creates a stream compatible with HTTP "Content-encoding: gzip"
#define OS_CODE   0x03

Functions

int vsnprintf (char *buf, size_t max, const char *fmt, va_list argp)
 vsnprintf wrapper
void * context_loop (int)
 handle one request This loop gets called once for every HTTP connection made to WebCit. At this entry point we have an HTTP socket with a browser allegedly on the other end, but we have not yet bound to a WebCit session.
void * housekeeping_loop (void)
 Wake up occasionally and clean house.
int ig_tcp_server (char *ip_addr, int port_number, int queue_len)
 This is a generic function to set up a master socket for listening on a TCP port. The server shuts down if the bind fails.
int ig_uds_server (char *sockpath, int queue_len)
 Create a Unix domain socket and listen on it.
int client_read_to (int sock, char *buf, int bytes, int timeout)
 Read data from the client socket.
ssize_t client_write (const void *buf, size_t count)
 write data to the client
void begin_burst (void)
 what burst???
int ZEXPORT compress_gzip (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen, int level)
void end_burst (void)
 what burst???
int client_read (int sock, char *buf, int bytes)
 Read data from the client socket with default timeout. (This is implemented in terms of client_read_to() and could be justifiably moved out of sysdep.c).
int client_getln (int sock, char *buf, int bufsiz)
 Get a LF-terminated line of text from the client. (This is implemented in terms of client_read() and could be justifiably moved out of sysdep.c).
void graceful_shutdown (int signum)
void start_daemon (char *pid_file)
 Start running as a daemon.
void spawn_another_worker_thread ()
 Spawn an additional worker thread into the pool.
int main (int argc, char **argv)
 Here's where it all begins.
void worker_entry (void)
int lprintf (int loglevel, const char *format,...)
 logprintf. log messages logs to stderr if loglevel is lower than the verbosity set at startup
void wc_backtrace (void)
 print the actual stack frame.

Variables

int verbosity = 9
int msock
int is_https = 0
int follow_xff = 0
int home_specified = 0
pthread_mutex_t SessionListMutex
pthread_key_t MyConKey
char socket_dir [PATH_MAX]
char * static_dirs []
char * static_content_dirs []
int ndirs = 3
char * server_cookie = NULL
int http_port = PORT_NUM
char * ctdlhost = DEFAULT_HOST
char * ctdlport = DEFAULT_PORT
int setup_wizard = 0
char wizard_filename [PATH_MAX]
int running_as_daemon = 0
pid_t current_child
 shut us down the regular way. param signum the signal we want to forward

Detailed Description

waiting on the specified port for incoming HTTP connections. When a connection is established, it calls context_loop() from context_loop.c.

Define Documentation

#define DEF_MEM_LEVEL   8

uses the same calling syntax as compress2(), but it creates a stream compatible with HTTP "Content-encoding: gzip"

memlevel???

Definition at line 292 of file webserver.c.

#define OS_CODE   0x03

unix

Definition at line 293 of file webserver.c.


Function Documentation

int vsnprintf ( char *  buf,
size_t  max,
const char *  fmt,
va_list  argp 
)

vsnprintf wrapper

Parameters:
buf the output charbuffer
max maximal size of the buffer
fmt the formatstring (see man printf)
argp the variable argument list

Definition at line 60 of file snprintf.c.

References lprintf().

Referenced by serv_printf(), snprintf(), svprintf(), and wprintf().

void* context_loop ( int  sock  ) 

handle one request This loop gets called once for every HTTP connection made to WebCit. At this entry point we have an HTTP socket with a browser allegedly on the other end, but we have not yet bound to a WebCit session.

The job of this function is to locate the correct session and bind to it, or create a session if necessary and bind to it, then run the WebCit transaction loop. Afterwards, we unbind from the session. When this function returns, the worker thread is then free to handle another transaction.

Parameters:
sock the socket we will put our answer to

Find out what it is that the web browser is asking for

Can we compress?

Browser-based sessions use cookies for session authentication

GroupDAV-based sessions use HTTP authentication

Read in the request

If the request is prefixed by "/webcit" then chop that off. This allows a front end web server to forward all /webcit requests to us while still using the same web server port for other things.

Begin parsing the request.

Check for bogus requests

Strip out the method, leaving the URL up front...

While we're at it, gracefully handle requests for the robots.txt and favicon.ico files.

These are the URL's which may be executed without a session cookie already set. If it's not one of these, force the session to close because cookies are probably disabled on the client browser.

See if there's an existing session open with the desired ID or user/pass

If HTTP-AUTH, look for a session with matching credentials

If cookie-session, look for a session with matching session ID

Create a new session if we have to

A future improvement might be to check the session integrity at this point before continuing.

Bind to the session and perform the transaction

Free the request buffer

Free up any session-local substitution variables which were set during this transaction

Definition at line 275 of file context_loop.c.

References cookie_to_stuff(), CtdlDecodeBase64(), extract_token(), wcsession::httpauth_pass, wcsession::httpauth_user, httpdate_to_timestamp(), if_modified_since, is_bogus(), httprequest::line, lprintf(), wcsession::next, httprequest::next, remove_token(), req_gets(), safestrncpy(), SessionList, SessionListMutex, and wcsession::wc_session.

Referenced by worker_entry().

int ig_tcp_server ( char *  ip_addr,
int  port_number,
int  queue_len 
)

This is a generic function to set up a master socket for listening on a TCP port. The server shuts down if the bind fails.

Parameters:
ip_addr ip to bind to
port_number the port to bind to
queue_len the size of the input queue ????

Set some socket options that make sense.

Definition at line 75 of file webserver.c.

References lprintf().

Referenced by main().

int ig_uds_server ( char *  sockpath,
int  queue_len 
)

Create a Unix domain socket and listen on it.

Parameters:
sockpath file name of the unix domain socket
queue_len queue size of the kernel fifo????

Definition at line 125 of file webserver.c.

References lprintf(), and safestrncpy().

Referenced by main().

int client_read_to ( int  sock,
char *  buf,
int  bytes,
int  timeout 
)

Read data from the client socket.

Parameters:
sock socket fd to read from ???
buf buffer to read into
bytes how large is the read buffer?
timeout how long should we wait for input?
Returns:
values are\ 1 Requested number of bytes has been read.\ 0 Request timed out.\ -1 Connection is broken, or other error.

Definition at line 183 of file webserver.c.

References client_read_ssl(), is_https, and lprintf().

Referenced by client_read().

ssize_t client_write ( const void *  buf,
size_t  count 
)

write data to the client

Parameters:
buf data to write to the client
count size of buffer

Definition at line 232 of file webserver.c.

References client_write_ssl(), and is_https.

Referenced by display_floorconfig(), display_siteconfig(), end_burst(), http_transmit_thing(), output_html(), and wprintf().

int ZEXPORT compress_gzip ( Bytef *  dest,
uLongf *  destLen,
const Bytef *  source,
uLong  sourceLen,
int  level 
)

gzip magic header

write gzip header

xflags

Parameters:
dest  compressed buffer
destLen  length of the compresed data
source  source to encode
sourceLen  length of the source to encode
level  what level???

Definition at line 294 of file webserver.c.

Referenced by end_burst(), and http_transmit_thing().

int client_read ( int  sock,
char *  buf,
int  bytes 
)

Read data from the client socket with default timeout. (This is implemented in terms of client_read_to() and could be justifiably moved out of sysdep.c).

Parameters:
sock the socket fd to read from???
buf the buffer to write to
bytes how large is the buffer

Definition at line 408 of file webserver.c.

References client_read_to().

Referenced by client_getln(), and session_loop().

int client_getln ( int  sock,
char *  buf,
int  bufsiz 
)

Get a LF-terminated line of text from the client. (This is implemented in terms of client_read() and could be justifiably moved out of sysdep.c).

Parameters:
sock socket fd to get client line from???
buf buffer to write read data to
bufsiz how many bytes to read
Returns:
numer of bytes read???

Read one character at a time.

Non printable character recieved from client

If we got a long line, discard characters until the newline.

Strip any trailing non-printable characters.

Definition at line 423 of file webserver.c.

References client_read().

Referenced by req_gets().

void start_daemon ( char *  pid_file  ) 

Start running as a daemon.

param do_close_stdio Only close stdio if set.

Definition at line 473 of file webserver.c.

References graceful_shutdown().

Referenced by main().

void spawn_another_worker_thread ( void   ) 

Spawn an additional worker thread into the pool.

< Thread descriptor

< Thread attributes

set attributes for the new thread

Our per-thread stacks need to be bigger than the default size, otherwise the MIME parser crashes on FreeBSD, and the IMAP service crashes on 64-bit Linux.

now create the thread

free up the attributes

Definition at line 566 of file webserver.c.

References lprintf(), and worker_entry().

Referenced by main().

int main ( int  argc,
char **  argv 
)

Here's where it all begins.

Parameters:
argc number of commandline args
argv the commandline arguments

< Thread descriptor

< Thread attributes

< General-purpose variables

< listen on a unix domain socket?

Parse command line

Tell 'em who's in da house

initialize the International Bright Young Thing

we should go somewhere we can leave our coredump, if enabled...

Set up a place to put thread-specific data. We only need a single pointer per thread - it points to the wcsession struct to which the thread is currently bound.

Set up a place to put thread-specific SSL data. We don't stick this in the wcsession struct because SSL starts up before the session is bound, and it gets torn down between transactions.

Bind the server to our favorite port. There is no need to check for errors, because ig_tcp_server() exits if it doesn't succeed.

Start up the housekeeping thread

If this is an HTTPS server, fire up SSL

Start a few initial worker threads

Definition at line 605 of file webserver.c.

References follow_xff, home_specified, housekeeping_loop(), ig_tcp_server(), ig_uds_server(), init_ssl(), initialize_axdefs(), initialize_locales(), initialize_viewdefs(), is_https, lprintf(), msock, MyConKey, safestrncpy(), SessionListMutex, socket_dir, spawn_another_worker_thread(), start_daemon(), ThreadSSL, verbosity, and worker_entry().

void worker_entry ( void   ) 

Entry point for worker threads

Only one thread can accept at a time

Set the SO_REUSEADDR socket option

If we are an HTTPS server, go crypto now.

Perform an HTTP transaction...

Shut down SSL/TLS if required...

...and close the socket.

Definition at line 836 of file webserver.c.

References context_loop(), endtls(), is_https, lingering_close(), lprintf(), msock, and starttls().

Referenced by main(), and spawn_another_worker_thread().

int lprintf ( int  loglevel,
const char *  format,
  ... 
)

logprintf. log messages logs to stderr if loglevel is lower than the verbosity set at startup

Parameters:
loglevel level of the message
format the printf like format string
... the strings to put into format

Definition at line 896 of file webserver.c.

References verbosity.

Referenced by cal_process_object(), client_read_ssl(), client_read_to(), client_write_ssl(), context_loop(), display_rss(), do_freebusy(), endtls(), httplang_to_locale(), ical_dezonify(), ical_dezonify_backend(), ical_encapsulate_subcomponent(), ig_tcp_server(), ig_uds_server(), init_ssl(), initialize_locales(), main(), output_html(), output_static(), post_message(), pullquote_message(), read_message(), read_server_binary(), save_individual_event(), save_individual_task(), serv_getln(), serv_printf(), serv_puts(), serv_read(), serv_write(), session_loop(), spawn_another_worker_thread(), starttls(), tcp_connectsock(), timeout(), uds_connectsock(), upload_handler(), vsnprintf(), wc_backtrace(), and worker_entry().


Variable Documentation

int verbosity = 9

Logging level

Definition at line 23 of file webserver.c.

Referenced by lprintf(), and main().

int msock

master listening socket

Definition at line 24 of file webserver.c.

Referenced by main(), and worker_entry().

int is_https = 0

Nonzero if I am an HTTPS service

Definition at line 25 of file webserver.c.

Referenced by client_read_to(), client_write(), display_editroom(), display_generic(), display_rss(), do_listsub(), main(), and worker_entry().

int follow_xff = 0

Follow X-Forwarded-For: header

Definition at line 26 of file webserver.c.

Referenced by main(), and session_loop().

int home_specified = 0

did the user specify a homedir?

Definition at line 27 of file webserver.c.

Referenced by main().

pthread_mutex_t SessionListMutex

Only one thread may manipulate SessionList at a time...

Definition at line 18 of file context_loop.c.

Referenced by context_loop(), do_housekeeping(), and main().

pthread_key_t MyConKey

TSD key for MySession()

Definition at line 22 of file context_loop.c.

Referenced by main().

char socket_dir[PATH_MAX]

where to talk to our citadel server

Definition at line 33 of file webserver.c.

Referenced by main().

char* static_dirs[]

Initial value:

{ 
        (char*)static_dir,                  
        (char*)static_local_dir,            
        (char*)editor_absolut_dir           
}
Web representation

Definition at line 37 of file webserver.c.

Referenced by session_loop().

char* static_content_dirs[]

Initial value:

 {
        "static",                     
        "static.local",               
        "tiny_mce"                    
}
Subdirectories from which the client may request static content

(If you add more, remember to increment 'ndirs' below)

Definition at line 48 of file webserver.c.

Referenced by session_loop().

char* server_cookie = NULL

our Cookie connection to the client

Definition at line 57 of file webserver.c.

Referenced by output_headers().

int http_port = PORT_NUM

Port to listen on

Definition at line 59 of file webserver.c.

char* ctdlhost = DEFAULT_HOST

our name

Definition at line 61 of file webserver.c.

Referenced by do_welcome(), init_ssl(), session_loop(), and setup_chat_socket().

char* ctdlport = DEFAULT_PORT

our Port

Definition at line 62 of file webserver.c.

Referenced by do_welcome(), init_ssl(), session_loop(), and setup_chat_socket().

int setup_wizard = 0

should we run the setup wizard?

Todo:

Definition at line 63 of file webserver.c.

Referenced by do_welcome().

char wizard_filename[PATH_MAX]

where's the setup wizard?

Definition at line 64 of file webserver.c.

Referenced by do_welcome().

int running_as_daemon = 0

should we deamonize on startup?

Definition at line 65 of file webserver.c.


Generated on Wed Jun 20 23:13:15 2007 for webcit by  doxygen 1.5.2