Sort
Implement Client-Server Model
Overview
Separate the CgiApplication class into two classes:
- CgiRequestServer
- CgiRequestClient
Rather than using an open standard like FastCGI, with very advanced capabilities, I think I'd rather keep this project simple and re-use my existing code.
Requirements:
CgiRequestClient
- Initialized by HTTP server with environment containing REQUEST_URI
- Does not need to call
::ImportRoutes()
or worry about handling routes. - Log file: /var/log/CgiRequestClient.log
Program Flow
- Launched by HTTP Server with environment data
- Check for server .PID file and .socket.
- log all errors
- if .pid file is missing, and socket is still there, log it too!
- exit if things look fishy, and return an error!
- (optional) output something on stdout instead of just returning an error
- Create objects and call methods to process the environment information
- connect to the server socket and send envp information, including REQUEST_URI
- Wait for result.
- Spit result back at HTTP server.
CgiRequestServer
- Socket Path: /tmp/CgiRequestServer.sock
- PID File Path: /tmp/CgiRequestServer.pid
- Log file: /var/log/CgiRequestServer.log
Program Flow
- Check for a .pid file to prevent duplicate launches
- (optional) also search for processes by name
- Check for a stale socket and remove if needed.
- Create named socket at /tmp
- Daemonize self
- Parent Process:
- Calls
fork()
,exec()
anddaemon()
as required - Close all file descriptors
- Log all errors and successes, flush file, close it.
- Calls
- Child Process:
- Write to log as soon as forked.
- Registers signal handlers (maybe optional)
- Redirect stdout and stderr to /dev/null
- Write a .PID file in same dir as socket
- Write PID to log.
- Normal CgiApplication initialization.
- Scan markdown content directory with MarkdownRouteHandler::GenerateRoutes()
- Creates infinite loop listening on socket
- On accept:
- Create a thread containing the new accept socket
- Associate the thread with REQUEST_URI in a Dictionary object.
- Process the REQUEST_URI using the cached routes and IRouteHandler objects in memory
- Parent Process:
Implementation Proposal
- Can re-use the app initialization code form CgiApplication
- Proposed split:
CgiApplication
->
ICgiApplication + ICgiRequestHandler
,CgiApplicationBase
,CgiRequestHandler
CGiApplicationBase
: initializes args, env etcCgiRequestHandlerBase
: Route-Handling functionality
- Final implementation classes can handle daemonizing, thread-management, etc.