arsd.cgi

Provides a uniform server-side API for CGI, FastCGI, SCGI, and HTTP web applications.

import arsd.cgi;

// Instead of writing your own main(), you should write a function
// that takes a Cgi param, and use mixin GenericMain
// for maximum compatibility with different web servers.
void hello(Cgi cgi) {
	cgi.setResponseContentType("text/plain");

	if("name" in cgi.get)
		cgi.write("Hello, " ~ cgi.get["name"]);
	else
		cgi.write("Hello, world!");
}

mixin GenericMain!hello;

Test on console (works in any interface mode):

$ ./cgi_hello GET / name=whatever

If using http version (default on dub builds, or on custom builds when passing -version=embedded_httpd to dmd):

$ ./cgi_hello --port 8080
# now you can go to http://localhost:8080/?name=whatever

Public Imports

std.string
public import std.string;
Undocumented in source.
std.stdio
public import std.stdio;
Undocumented in source.
std.conv
public import std.conv;
Undocumented in source.

Members

Aliases

CMT
alias CMT = void function(Socket)
Undocumented in source.
CgiConnectionHandle
alias CgiConnectionHandle = HANDLE
Undocumented in source.
CgiConnectionHandle
alias CgiConnectionHandle = SOCKET
Undocumented in source.
CgiConnectionHandle
alias CgiConnectionHandle = void*
Undocumented in source.
CgiConnectionHandle
alias CgiConnectionHandle = SOCKET
Undocumented in source.
CgiConnectionHandle
alias CgiConnectionHandle = int
Undocumented in source.
FCGX_ParamArray
alias FCGX_ParamArray = char**
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
GetTempPath
alias GetTempPath = GetTempPathW
Undocumented in source.
LocalServerConnectionHandle
alias LocalServerConnectionHandle = HANDLE
Undocumented in source.
LocalServerConnectionHandle
alias LocalServerConnectionHandle = int
Undocumented in source.
SocketConnectionHandle
alias SocketConnectionHandle = SOCKET
Undocumented in source.
SocketConnectionHandle
alias SocketConnectionHandle = int
Undocumented in source.
c_int
alias c_int = long
Undocumented in source.
c_int
alias c_int = int
Undocumented in source.
setmode
alias setmode = _setmode
Undocumented in source.

Classes

BasicDataServerConnection
class BasicDataServerConnection
Undocumented in source.
BasicDataServerImplementation
class BasicDataServerImplementation
Undocumented in source.
BasicDataServerSession
class BasicDataServerSession(Data)

An implementation of Session that works on real cgi connections utilizing the BasicDataServer.

BufferedInputRange
class BufferedInputRange

This is NOT ACTUALLY an input range! It is too different. Historical mistake kinda.

Cgi
class Cgi

The main interface with the web request

CgiTester
class CgiTester

A helper test class for request handler unittests.

CollectionOf
class CollectionOf(Obj)

Base class for REST collections.

ConnectionClosedException
class ConnectionClosedException

thrown when a connection is closed remotely while we waiting on data from it

ConnectionException
class ConnectionException
Undocumented in source.
ConnectionThread
class ConnectionThread
Undocumented in source.
EventSourceServerImplementation
class EventSourceServerImplementation
ListeningConnectionManager
class ListeningConnectionManager

To use this thing:

MissingArgumentException
class MissingArgumentException
Undocumented in source.
MockSession
class MockSession(Data)

A mock object that works like the real session, but doesn't actually interact with any actual database or http connection. Simply stores the data in its instance members.

RestObject
class RestObject(CRTP)

The base of all REST objects, to be used with serveRestObject and serveRestCollectionOf.

ScheduledJobServerConnection
class ScheduledJobServerConnection
Undocumented in source.
ScheduledJobServerImplementation
class ScheduledJobServerImplementation
Undocumented in source.
WebObject
class WebObject

The base class for the dispatcher function and object support.

WebPresenter
class WebPresenter(CRTP)

A web presenter is responsible for rendering things to HTML to be usable in a web browser.

WebSocket
class WebSocket

WEBSOCKET SUPPORT:

Enums

AccessCheck
enum AccessCheck
Undocumented in source.
AddTrailingSlash
enum AddTrailingSlash
Undocumented in source.
Operation
enum Operation
Undocumented in source.
UpdateResult
enum UpdateResult
Undocumented in source.
ValidationResult
enum ValidationResult
Undocumented in source.
WebSocketOpcode
enum WebSocketOpcode
Undocumented in source.

Functions

FCGX_Accept
c_int FCGX_Accept(FCGX_Stream** stdin, FCGX_Stream** stdout, FCGX_Stream** stderr, FCGX_ParamArray* envp)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
FCGX_Accept_r
int FCGX_Accept_r(FCGX_Request* request)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
FCGX_FFlush
c_int FCGX_FFlush(FCGX_Stream* stream)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
FCGX_GetChar
c_int FCGX_GetChar(FCGX_Stream* stream)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
FCGX_HasSeenEOF
int FCGX_HasSeenEOF(FCGX_Stream* stream)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
FCGX_Init
void FCGX_Init()
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
FCGX_InitRequest
int FCGX_InitRequest(FCGX_Request* request, int sock, int flags)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
FCGX_OpenSocket
int FCGX_OpenSocket(char* , int )
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
FCGX_PutStr
c_int FCGX_PutStr(ubyte* str, c_int n, FCGX_Stream* stream)
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
GetTempPathW
DWORD GetTempPathW(DWORD , LPWSTR )
Undocumented in source but is binding to Windows. You might be able to learn more by searching the web for its name.
_setmode
int _setmode(int , int )
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
acceptWebsocket
WebSocket acceptWebsocket(Cgi cgi)
Undocumented in source. Be warned that the author may not have intended to support it.
allocateIoOp
IoOp* allocateIoOp(int fd, int operation, int bufferSize, bool delegate(IoOp*, int) handler)
Undocumented in source. Be warned that the author may not have intended to support it.
beautify
string beautify(string name, char space, bool allLowerCase)
Undocumented in source. Be warned that the author may not have intended to support it.
byChunk
ByChunkRange byChunk(const(ubyte)[] data)
Undocumented in source. Be warned that the author may not have intended to support it.
byChunk
ByChunkRange byChunk(BufferedInputRange ir, size_t atMost)
Undocumented in source. Be warned that the author may not have intended to support it.
callFromCgi
auto callFromCgi(T dg, Cgi cgi)
Undocumented in source. Be warned that the author may not have intended to support it.
cgiMainImpl
void cgiMainImpl(string[] args)
Undocumented in source. Be warned that the author may not have intended to support it.
closeLocalServerConnection
void closeLocalServerConnection(LocalServerConnectionHandle handle)
Undocumented in source. Be warned that the author may not have intended to support it.
consume
T[] consume(T[] range, int count)
Undocumented in source. Be warned that the author may not have intended to support it.
contentTypeFromFileExtension
string contentTypeFromFileExtension(string filename)
Undocumented in source. Be warned that the author may not have intended to support it.
dTimeToSysTime
SysTime dTimeToSysTime(long dTime, TimeZone tz)
Undocumented in source. Be warned that the author may not have intended to support it.
dateTimeToDTime
long dateTimeToDTime(DateTime dt)
Undocumented in source. Be warned that the author may not have intended to support it.
dechunk
immutable(ubyte[]) dechunk(BufferedInputRange ir)
Undocumented in source. Be warned that the author may not have intended to support it.
decodeVariables
string[][string] decodeVariables(string data, string separator, string[]* namesInOrder, string[]* valuesInOrder)

breaks down a url encoded string

decodeVariablesSingle
string[string] decodeVariablesSingle(string data)

breaks down a url encoded string, but only returns the last value of any array

defaultFormat
string defaultFormat()
Undocumented in source. Be warned that the author may not have intended to support it.
dispatchRpcServer
void dispatchRpcServer(Class this_, ubyte[] data, int fd)
Undocumented in source. Be warned that the author may not have intended to support it.
dispatchTo
auto dispatchTo(string urlPrefix)

Dispatches the URL (and anything under it) to another dispatcher function. The function should look something like this:

doThreadHttpConnection
void doThreadHttpConnection(Socket connection)
Undocumented in source. Be warned that the author may not have intended to support it.
doThreadScgiConnection
void doThreadScgiConnection(Socket connection)
Undocumented in source. Be warned that the author may not have intended to support it.
dummyCgi
Cgi dummyCgi(Cgi.RequestMethod method, string url, ubyte[] data, void delegate(const(ubyte)[]) outputSink)

use this for testing or other isolated things when you want it to be no-ops

encodeVariables
string encodeVariables(string[string] data)

url encodes the whole string

encodeVariables
string encodeVariables(string[][string] data)

url encodes a whole string

freeIoOp
void freeIoOp(IoOp* ptr)
Undocumented in source. Be warned that the author may not have intended to support it.
getHttpCodeText
string getHttpCodeText(int code)
Undocumented in source. Be warned that the author may not have intended to support it.
getTempDirectory
string getTempDirectory()
Undocumented in source. Be warned that the author may not have intended to support it.
getUtcTime
long getUtcTime()
Undocumented in source. Be warned that the author may not have intended to support it.
hackAroundLinkerError
void hackAroundLinkerError()
Undocumented in source. Be warned that the author may not have intended to support it.
handleException
bool handleException(Cgi cgi, Throwable t)
Undocumented in source. Be warned that the author may not have intended to support it.
handleWith
auto handleWith(string urlPrefix)

Dispatches the URL to a specific function.

hasIfCalledFromWeb
bool hasIfCalledFromWeb()
Undocumented in source. Be warned that the author may not have intended to support it.
isCgiRequestMethod
bool isCgiRequestMethod(string s)
Undocumented in source. Be warned that the author may not have intended to support it.
isInvalidHandle
bool isInvalidHandle(CgiConnectionHandle h)
Undocumented in source. Be warned that the author may not have intended to support it.
locationOf
int locationOf(T[] data, string item)
Undocumented in source. Be warned that the author may not have intended to support it.
makeChunk
const(ubyte)[] makeChunk(const(ubyte)[] data)
Undocumented in source. Be warned that the author may not have intended to support it.
makeDataUrl
string makeDataUrl(string mimeType, void[] data)

Makes a data:// uri that can be used as links in most newer browsers (IE8+).

messageFromException
string messageFromException(Throwable t)
Undocumented in source. Be warned that the author may not have intended to support it.
mustNotBeSetFromWebParams
bool mustNotBeSetFromWebParams()
Undocumented in source. Be warned that the author may not have intended to support it.
nextPieceFromSlash
string nextPieceFromSlash(string remainingUrl)
Undocumented in source. Be warned that the author may not have intended to support it.
nonBlockingWrite
void nonBlockingWrite(EventIoServer eis, int connection, void[] data)
Undocumented in source. Be warned that the author may not have intended to support it.
nonBlockingWrite
void nonBlockingWrite(EventIoServer eis, int connection, void[] data)
Undocumented in source. Be warned that the author may not have intended to support it.
openLocalServerConnection
LocalServerConnectionHandle openLocalServerConnection(string name)
Undocumented in source. Be warned that the author may not have intended to support it.
plainHttpError
string plainHttpError(bool isCgi, string type, Throwable t)
Undocumented in source. Be warned that the author may not have intended to support it.
printDate
string printDate(DateTime date)
Undocumented in source. Be warned that the author may not have intended to support it.
rawurlencode
string rawurlencode(char[] data)

Encodes all but the explicitly unreserved characters per rfc 3986 Alphanumeric and -_.~ are the only ones left unencoded name is borrowed from php

read_fd
ssize_t read_fd(int fd, void* ptr, size_t nbytes, int* recvfd)
Undocumented in source. Be warned that the author may not have intended to support it.
restObjectServeHandler
bool restObjectServeHandler(Cgi cgi, Presenter presenter, string url)
Undocumented in source. Be warned that the author may not have intended to support it.
runAddonServer
void runAddonServer(string localListenerName, EIS eis)
Undocumented in source. Be warned that the author may not have intended to support it.
runEventServer
void runEventServer()
Undocumented in source. Be warned that the author may not have intended to support it.
runSessionServer
void runSessionServer()
Undocumented in source. Be warned that the author may not have intended to support it.
runTimerServer
void runTimerServer()
Undocumented in source. Be warned that the author may not have intended to support it.
runWebsocketServer
void runWebsocketServer()
Undocumented in source. Be warned that the author may not have intended to support it.
sendAll
void sendAll(Socket s, const(void)[] data, string file, size_t line)
Undocumented in source. Be warned that the author may not have intended to support it.
sendToWebsocketServer
void sendToWebsocketServer(string content, string group)
Undocumented in source. Be warned that the author may not have intended to support it.
sendToWebsocketServer
void sendToWebsocketServer(WebSocket ws, string group)
Undocumented in source. Be warned that the author may not have intended to support it.
serveApi
auto serveApi(string urlPrefix)

Serves a class' methods, as a kind of low-state RPC over the web. To be used with dispatcher.

serveApiInternal
auto serveApiInternal(string urlPrefix)
Undocumented in source. Be warned that the author may not have intended to support it.
serveRedirect
auto serveRedirect(string urlPrefix, string redirectTo, int code)

Redirects one url to another

serveRestObject
auto serveRestObject(string urlPrefix)

Serves a REST object, similar to a Ruby on Rails resource.

serveStaticFile
auto serveStaticFile(string urlPrefix, string filename, string contentType)

Serves a static file. To be used with dispatcher.

serveStaticFileDirectory
auto serveStaticFileDirectory(string urlPrefix, string directory)

This serves a directory full of static files, figuring out the content-types from file extensions. It does not let you to descend into subdirectories (or ascend out of it, of course)

setmode
int setmode(int , int )
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
startWebsocketServer
void startWebsocketServer()
Undocumented in source. Be warned that the author may not have intended to support it.
sysTimeToDTime
long sysTimeToDTime(SysTime sysTime)
Undocumented in source. Be warned that the author may not have intended to support it.
toHex
string toHex(long num)
Undocumented in source. Be warned that the author may not have intended to support it.
toHexUpper
string toHexUpper(long num)
Undocumented in source. Be warned that the author may not have intended to support it.
urlNamesForMethod
string[] urlNamesForMethod(string def)
Undocumented in source. Be warned that the author may not have intended to support it.
urlify
string urlify(string name)
Undocumented in source. Be warned that the author may not have intended to support it.
websocketRequested
bool websocketRequested(Cgi cgi)
Undocumented in source. Be warned that the author may not have intended to support it.
write_fd
ssize_t write_fd(int fd, void* ptr, size_t nbytes, int sendfd)
Undocumented in source. Be warned that the author may not have intended to support it.

Interfaces

BasicDataServer
interface BasicDataServer

Direct interface to the basic data add-on server. You can typically use Cgi.getSessionObject as a more convenient interface.

ByChunkRange
interface ByChunkRange
Undocumented in source.
EventIoServer
interface EventIoServer

You can customize your server by subclassing the appropriate server. Then, register your subclass at compile time with the registerEventIoServer template, or implement your own main function and call it yourself.

EventSourceServer
interface EventSourceServer
ScheduledJobServer
interface ScheduledJobServer
Session
interface Session(Data)

A convenience object for talking to the BasicDataServer from a higher level. See: Cgi.getSessionObject.

SessionObject
interface SessionObject

Base for storing sessions in an array. Exists primarily for internal purposes and you should generally not use this.

Manifest constants

INVALID_CGI_CONNECTION_HANDLE
enum INVALID_CGI_CONNECTION_HANDLE;
Undocumented in source.
INVALID_CGI_CONNECTION_HANDLE
enum INVALID_CGI_CONNECTION_HANDLE;
Undocumented in source.
INVALID_CGI_CONNECTION_HANDLE
enum INVALID_CGI_CONNECTION_HANDLE;
Undocumented in source.
INVALID_CGI_CONNECTION_HANDLE
enum INVALID_CGI_CONNECTION_HANDLE;
Undocumented in source.
INVALID_CGI_CONNECTION_HANDLE
enum INVALID_CGI_CONNECTION_HANDLE;
Undocumented in source.

Mixin templates

CustomCgiMain
mixintemplate CustomCgiMain(CustomCgi, alias fun, long maxContentLength = defaultMaxContentLength)

If you want to use a subclass of Cgi with generic main, use this mixin.

ForwardCgiConstructors
mixintemplate ForwardCgiConstructors()

If you are doing a custom cgi class, mixing this in can take care of the required constructors for you

GenericMain
mixintemplate GenericMain(alias fun, long maxContentLength = defaultMaxContentLength)

Use this instead of writing your own main

ImplementRpcClientInterface
mixintemplate ImplementRpcClientInterface(T, string serverPath)
Undocumented in source.

Structs

DUMMY
struct DUMMY
Undocumented in source.
DefaultFormat
struct DefaultFormat

UDA: default format to respond for this method

DispatcherData
struct DispatcherData(Presenter)

Used exclusively with dispatchTo

DispatcherDefinition
struct DispatcherDefinition(alias dispatchHandler, DispatcherDetails = typeof(null))
Undocumented in source.
DisplayName
struct DisplayName

UDA: The name displayed to the user in auto-generated HTML.

FCGX_Request
struct FCGX_Request
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
FCGX_Stream
struct FCGX_Stream
Undocumented in source but is binding to C. You might be able to learn more by searching the web for its name.
IoOp
struct IoOp
Undocumented in source.
MultipleResponses
struct MultipleResponses(T...)

Can return one of the given types, decided at runtime. The syntax is to declare all the possible types in the return value, then you can return typeof(return)(...value...) to construct it.

RawResponse
struct RawResponse
Undocumented in source.
Redirection
struct Redirection

You can return this from WebObject subclasses for redirections.

ScheduledJobHelper
struct ScheduledJobHelper

See schedule to make one of these. You then call one of the methods here to set it up:

SerializationBuffer
struct SerializationBuffer
Undocumented in source.
Uri
struct Uri

Represents a url that can be broken down or built up through properties

UrlName
struct UrlName

UDA: The name used in the URL or web parameter.

WebSocketFrame
struct WebSocketFrame
Undocumented in source.
ifCalledFromWeb
struct ifCalledFromWeb(alias func)

This can be attached to any constructor or function called from the cgi system.

Templates

dispatcher
template dispatcher(definitions...)

A URL dispatcher.

schedule
template schedule(alias fn, T...)

First step to schedule a job on the scheduled job server.

Variables

defaultMaxContentLength
enum long defaultMaxContentLength;
Undocumented in source.
processPoolSize
int processPoolSize;
Undocumented in source.

Detailed Description

Compile versions

If you are using dub, use:

subConfiguration "arsd-official:cgi" "VALUE_HERE"

or to dub.json:

"subConfigurations": {"arsd-official:cgi": "VALUE_HERE"}

to change versions. The possible options for VALUE_HERE are:

  • embedded_httpd for the embedded httpd version (built-in web server). This is the default.
  • cgi for traditional cgi binaries.
  • fastcgi for FastCGI builds.
  • scgi for SCGI builds.

With dmd, use:

Interfaces(mutually exclusive)
-version=plain_cgiThe default building the module alone without dub - a traditional, plain CGI executable will be generated.
-version=embedded_httpdA HTTP server will be embedded in the generated executable. This is default when building with dub.
-version=fastcgiA FastCGI executable will be generated.
-version=scgiA SCGI (SimpleCGI) executable will be generated.
-version=embedded_httpd_threadsThe embedded HTTP server will use a single process with a thread pool. (use instead of plain embedded_httpd if you want this specific implementation)
-version=embedded_httpd_processesThe embedded HTTP server will use a prefork style process pool. (use instead of plain embedded_httpd if you want this specific implementation)
-version=embedded_httpd_processes_accept_after_forkIt will call accept() in each child process, after forking. This is currently the only option, though I am experimenting with other ideas. You probably should NOT specify this right now.
Tweaks(can be used together with others)
-version=cgi_with_websocketThe CGI class has websocket server support.
-version=with_opensslnot currently used
-version=cgi_embedded_sessionsThe session server will be embedded in the cgi.d server process
-version=cgi_session_server_processThe session will be provided in a separate process, provided by cgi.d.

Compile and run

For CGI, dmd yourfile.d cgi.d then put the executable in your cgi-bin directory.

For FastCGI: dmd yourfile.d cgi.d -version=fastcgi and run it. spawn-fcgi helps on nginx. You can put the file in the directory for Apache. On IIS, run it with a port on the command line.

For SCGI: dmd yourfile.d cgi.d -version=scgi and run the executable, providing a port number on the command line.

For an embedded HTTP server, run dmd yourfile.d cgi.d -version=embedded_httpd and run the generated program. It listens on port 8085 by default. You can change this on the command line with the --port option when running your program.

You can also simulate a request by passing parameters on the command line, like:

./yourprogram GET / name=adr

And it will print the result to stdout.

CGI Setup tips

On Apache, you may do SetHandler cgi-script in your .htaccess file.

Integration tips

cgi.d works well with dom.d for generating html. You may also use web.d for other utilities and automatic api wrapping.

dom.d usage:

import arsd.cgi;
import arsd.dom;

void hello_dom(Cgi cgi) {
	auto document = new Document();

	static import std.file;
	// parse the file in strict mode, requiring it to be well-formed UTF-8 XHTML
	// (You'll appreciate this if you've ever had to deal with a missing </div>
	// or something in a php or erb template before that would randomly mess up
	// the output in your browser. Just check it and throw an exception early!)
	//
	// You could also hard-code a template or load one at compile time with an
	// import expression, but you might appreciate making it a regular file
	// because that means it can be more easily edited by the frontend team and
	// they can see their changes without needing to recompile the program.
	//
	// Note on CTFE: if you do choose to load a static file at compile time,
	// you *can* parse it in CTFE using enum, which will cause it to throw at
	// compile time, which is kinda cool too. Be careful in modifying that document,
	// though, as it will be a static instance. You might want to clone on on demand,
	// or perhaps modify it lazily as you print it out. (Try element.tree, it returns
	// a range of elements which you could send through std.algorithm functions. But
	// since my selector implementation doesn't work on that level yet, you'll find that
	// harder to use. Of course, you could make a static list of matching elements and
	// then use a simple e is e2 predicate... :) )
	document.parseUtf8(std.file.read("your_template.html"), true, true);

	// fill in data using DOM functions, so placing it is in the hands of HTML
	// and it will be properly encoded as text too.
	//
	// Plain html templates can't run server side logic, but I think that's a
	// good thing - it keeps them simple. You may choose to extend the html,
	// but I think it is best to try to stick to standard elements and fill them
	// in with requested data with IDs or class names. A further benefit of
	// this is the designer can also highlight data based on sources in the CSS.
	//
	// However, all of dom.d is available, so you can format your data however
	// you like. You can do partial templates with innerHTML too, or perhaps better,
	// injecting cloned nodes from a partial document.
	//
	// There's a lot of possibilities.
	document["#name"].innerText = cgi.request("name", "default name");

	// send the document to the browser. The second argument to `cgi.write`
	// indicates that this is all the data at once, enabling a few small
	// optimizations.
	cgi.write(document.toString(), true);
}

Concepts: Input: Cgi.get, Cgi.post, Cgi.request, Cgi.files, Cgi.cookies, Cgi.pathInfo, Cgi.requestMethod, and HTTP headers (Cgi.headers, Cgi.userAgent, Cgi.referrer, Cgi.accept, Cgi.authorization, Cgi.lastEventId)

Output: Cgi.write, Cgi.header, Cgi.setResponseStatus, Cgi.setResponseContentType, Cgi.gzipResponse

Cookies: Cgi.setCookie, Cgi.clearCookie, Cgi.cookie, Cgi.cookies

Caching: Cgi.setResponseExpires, Cgi.updateResponseExpires, Cgi.setCache

Redirections: Cgi.setResponseLocation

Other Information: Cgi.remoteAddress, Cgi.https, Cgi.port, Cgi.scriptName, Cgi.requestUri, Cgi.getCurrentCompleteUri, Cgi.onRequestBodyDataReceived

Overriding behavior: Cgi.handleIncomingDataChunk, Cgi.prepareForIncomingDataChunks, Cgi.cleanUpPostDataState

Installing: Apache, IIS, CGI, FastCGI, SCGI, embedded HTTPD (not recommended for production use)

Guide for PHP users

If you are coming from PHP, here's a quick guide to help you get started:

<?php
	$foo = $_POST["foo"];
	$bar = $_GET["bar"];
	$baz = $_COOKIE["baz"];

	$user_ip = $_SERVER["REMOTE_ADDR"];
	$host = $_SERVER["HTTP_HOST"];
	$path = $_SERVER["PATH_INFO"];

	setcookie("baz", "some value");

	echo "hello!";
?>
import arsd.cgi;
void app(Cgi cgi) {
	string foo = cgi.post["foo"];
	string bar = cgi.get["bar"];
	string baz = cgi.cookies["baz"];

	string user_ip = cgi.remoteAddress;
	string host = cgi.host;
	string path = cgi.pathInfo;

	cgi.setCookie("baz", "some value");

	cgi.write("hello!");
}

mixin GenericMain!app

Array elements

In PHP, you can give a form element a name like "something[]", and then $_POST["something"] gives an array. In D, you can use whatever name you want, and access an array of values with the cgi.getArray["name"] and cgi.postArray["name"] members.

Databases

PHP has a lot of stuff in its standard library. cgi.d doesn't include most of these, but the rest of my arsd repository has much of it. For example, to access a MySQL database, download database.d and mysql.d from my github repo, and try this code (assuming, of course, your database is set up):

import arsd.cgi;
import arsd.mysql;

void app(Cgi cgi) {
	auto database = new MySql("localhost", "username", "password", "database_name");
	foreach(row; mysql.query("SELECT count(id) FROM people"))
		cgi.write(row[0] ~ " people in database");
}

mixin GenericMain!app;

Similar modules are available for PostgreSQL, Microsoft SQL Server, and SQLite databases, implementing the same basic interface.

See Also

You may also want to see arsd.dom, arsd.web, and arsd.html for more code for making web applications.

For working with json, try arsd.jsvar.

arsd.database, arsd.mysql, arsd.postgres, arsd.mssql, and arsd.sqlite can help in accessing databases.

If you are looking to access a web application via HTTP, try std.net.curl, arsd.curl, or arsd.http2.

Meta