| Up - Design | Back - Protocol |
servproc.c and
servproc.h files) are under BSD-style license, so
it's allowed to make derivative works and incorporate the code
in any applications without creating additional distribution
restrictions.
struct rline
struct rline{ /* request line */
long paramc; /* parameters counter*/
char **params; /* parameters*/
};
Common representation for line block
or parsed HTTP from data or the parsed parameters list from URL.
Used directly by the user's program and internally. While
params isn't declared const
modification of that data by the user is undesirable.
struct request
struct request{ /* parsed request */
/* Primary */
long buffsize; /* buffer size */
char *buffer; /* input buffer */
long id; /* request ID */
long reqtype; /* request type */
long rlsize; /* request line size */
char *rl; /* request line */
long nlines; /* number of lines in request */
struct rline *lines; /* request lines */
long databuffsize; /* data buffer size */
char *databuffer; /* data buffer */
int fd; /* file descriptor (if any) */
/* Secondary */
long nform; /* number of lines in form (if any) */
struct rline *form; /* lines from form (if any) */
long narg; /* number of arguments (if any) */
struct rline *arg; /* arguments (if any) */
const char *accept; /* "Accept" */
const char *user_agent; /* "User-Agent" */
const char *content_type; /* "Content-type" */
const char *server_software;/* server software */
const char *server_name; /* server hostname */
int server_port; /* server port */
const char *script_name; /* script name (if any) */
const char *script_name_resolved; /* resolved script name (if any) */
const char *query_string; /* query string (if any) */
const char *remote_addr; /* remote address (dot notation) (if any) */
const char *remote_host; /* remote address (host/domain) (if any) */
const char *remote_user; /* remote user (from security) (if any) */
int ver_major; /* HTTP major version number (if any) */
int ver_minor; /* HTTP minor version number (if any) */
int method; /* request method */
time_t ifmodifiedsince; /* "If-Modified-Since" */
int keepalive; /* keep-alive option requested */
struct http_server *httpserver; /* server */
struct httpclientsession *session; /* session (if any) */
const void *appinfo; /* application info (if any) */
struct request *next; /* next item pointer */
struct request *prev; /* previous item pointer */
};
Representation for the parsed request, sent from the HTTP
client. Allocated and returned by readrequest() or getrequest(),
should be deallocated by deleterequest(), used by
multiple functions and user's program.
All numeric fields are in local format,
NOT in network byte order. const
means const and what isn't const, is
still a bad idea to modify.
"Primary" data is received from the server, "Secondary" fields
are the result of parsing in parserequest()
readrequest() reads request
from the server and makes this structure without parsing forms
or arguments,
so narg, arg, nform,
form and session
are zeroed. parserequest() checks if
content_type is
"application/x-www-form-urlencoded" and
method is POST, and if so parses the
form. data is changed in the proces of parsing by
replacing field separators with zeroes and decoding URL-encoded
data.
parserequest() also parses
arguments the same way.
getrequest() calls readrequest(), then if it
succeeds, calls parserequest().
struct httpresponse
struct httpresponse{
long buffsize; /* allocated buffer size */
long datasize; /* data size */
char *buffer; /* buffer */
long id; /* request id */
int fd; /* file descriptor (if any) */
struct httpresponse *header;/* header of this response (if any) */
struct httpresponse *body; /* body of this response (if it's a header) */
};
Representation of the response, prepared to be sent to
the HTTP server or client. Response with the header is
represented by two
such structures -- for the response header and for response
body. It allows to make the header (by makestandardheader()
or makesessionheader())
after the response data is ready.
Allocated and returned by createresponse(),
should be deallocated by deleteresponse(), used by API
library functions.
struct multipartsegment
struct multipartsegment{
struct multipartresponse *parentresponse;
/* multipart response, this segment belongs to */
struct httpresponse *s_response; /* response segment body */
struct multipartresponse *m_response; /* response segment body (if multipart)*/
struct multipartsegment *next; /* next segment */
struct multipartsegment *prev; /* previous segment */
};
struct multipartresponse
struct multipartresponse{
long headerbuffsize; /* allocated header buffer size */
long headerdatasize; /* header data size */
long id; /* request id */
int fd; /* file descriptor (if any) */
char *headerbuffer; /* header buffer */
char *boundary; /* boundary buffer */
int boundarysize; /* size of the boundary buffer */
int nsegments; /* number of segments */
struct multipartsegment *first; /* first segment */
struct multipartsegment *last; /* last segment */
};
Representation of the MIME multipart response and its segment. Multipart
response consists of multiple segments that have headers (but
no code) and separated by boundary lines (see MIME RFC 1521). Multipart responses are primarily used for the
Netscape-specific server push with the
"multipart/x-mixed-replace" content type.
struct multipartresponse is allocated and returned
by createmultipartresponse()
and should be deallocated by deletemultipartresponse(),
struct multipartsegment is allocated and returned
by createmultipartsegment()
and should be deallocated by deletemultipartsegment().
makemultipartheader()
writes multipart response header in a way similar to makestandardheader(),
although it doesn't include Content-Length in the
header, so it can be called before the response is ready.
struct httpclientsession
struct httpclientsession{
struct request *currequest; /* current request */
char *useragent; /* "User-Agent" */
char *username; /* remote user (from security) (if any) */
int ver_major; /* HTTP major version number (if any) */
int ver_minor; /* HTTP minor version number (if any) */
long useragentcap; /* capabilities, not supported */
time_t created; /* created */
time_t lastaccessed; /* last accessed */
char *cookie; /* cookie assigned to the session */
int cookieinurl; /* cookie in URL for non-cookie-supporting
browsers */
struct http_server *httpserver; /* server */
const void *appinfo; /* application info (if any) */
struct httpclientsession *next; /* next item pointer */
struct httpclientsession *prev; /* previous item pointer */
};
Representation of the session, attached to some session
identifier, included in URL or used as HTTP cookie. Created by
createsession(),
should be deallocated by deletesession(), used by
multiple session-supporting functions.
Session support is provided, but it isn't mandatory for the API functionality, and it doesn't disallow the program to use its own means to identify the user or session.
Two pointers to other struct
httpclientsession's are used to support the list of
sessions, they are handled automatically and should not be used
by the user's program.
struct http_server
struct http_server{
int infd; /* file descriptors, connected to server */
int outfd;
char *app_progname;
char *app_name;
char *app_hostname;
char *app_username;
char *app_password;
int app_port;
int app_hostname_allocated;
int app_username_allocated;
int app_password_allocated;
struct httpclientsession *client_sessions;
struct httpclientsession *client_last_session;
struct request *requests;
struct request *lastrequest;
int session_created; /* will be 1 after findsession(???,1),
if the session was created */
int session_expired; /* will be 1 after findsession(),
if the cookie from expired session was found */
long capabilities;
int exitflag;
int use_af_unix_socket; /* use AF_UNIX socket */
int *connectionfds;
int connectionfdshead;
int connectionfdstail;
int connectionfdscount;
int connectionfdsallocated;
};
Representation of the HTTP server. Usually only one server is
used, although multiple ones are allowed. Allocated by createserver(), deallocated
by deleteserver() and usually
initialized by servproc_init().
struct http_server *createserver(void);void deleteserver(struct http_server *server);struct request *readrequest(struct
http_server *server);
void parserequest(struct request *req);
struct request *getrequest(struct
http_server *server);
void deleterequest(struct
request *req);
void sendreply(struct
http_server *server,long opnum,long size,long id,char *s);
void sendlinereply(struct
http_server *server,long opnum,long id,char *s);
void finishreply(struct
http_server *server,long id);
void finishdropreply(struct
http_server *server,long id);
void setcapabilities(struct
http_server *server,long newcap);
void requestexit(struct
http_server *server,int newexitflag);
struct httpresponse *createresponse(int initsize,long id,int fd,int withheader);
void deleteresponse(struct httpresponse *response);
int writetoresponse(struct httpresponse *response,const char *buffer,long l);
int putlinetoresponse(struct httpresponse *response,const char *buffer);
int putmlinetoresponse(struct httpresponse *response,const char *buffer);
int putulinetoresponse(struct httpresponse *response,const char *buffer,char except);
int puturltoresponse(struct httpresponse *response,struct httpclientsession *session,const char *protocol,const char *host,int port,const char *path,const char *arg,const char *fragment);
int putlinetoheader(struct httpresponse *response,const char *buffer);
int makemyurl(char *dst,struct request *req,int maxsize,
struct httpclientsession *session,char *arg);
int putmyurltoresponse(struct httpresponse *response,struct request *req,struct httpclientsession *session,char *arg);
int putmyurltoheader(struct httpresponse *response,struct request *req,struct httpclientsession *session,char *arg);
int makestandardheader(struct httpresponse *response,int code,const char *contenttype,const char *servername,int keepalive);
int makesessionheader(struct httpresponse *response,int code,const char *contenttype,const char *servername,struct httpclientsession *session,int keepalive);
int sendresponse(struct
http_server *server,struct httpresponse *response);
int sendshortresponse(struct
http_server *server,struct httpresponse *response);
int finishresponse(struct
http_server *server,struct httpresponse *response);
int finishdropresponse(struct
http_server *server,struct httpresponse *response);
int redirect(struct request *req,char *destination);
struct multipartresponse *createmultipartresponse(long id,int fd);void deletemultipartresponse(struct multipartresponse *response,int clean);struct multipartsegment *createmultipartsegment(struct multipartresponse *response,struct httpresponse *responsebody);void deletemultipartsegment(struct multipartsegment *segment,int clear);int writetomultipartresponseheader(struct multipartresponse *response,const char *buffer,long l);int putlinetomultipartheader(struct multipartresponse *response,
const char *buffer);int makemultipartheader(struct multipartresponse *response,int code,
const char *contenttype,const char *servername,int keepalive);int sendmultipartresponseheader(struct http_server *server,struct multipartresponse *response);int sendmultipartresponsesegment(struct http_server *server,struct multipartsegment *segment);int sendmultipartresponsefinish(struct http_server *server,struct multipartresponse *response);int sendmultipartresponse(struct http_server *server,struct multipartresponse *response);struct httpclientsession *createsession(struct request *req,const void *appinfo,char *cookie);
int selectinstancesession(,struct httpclientsession *session);
int deselectinstancesession(struct httpclientsession *session);
int redirecttosession(struct httpclientsession *session);
void deletesession(struct httpclientsession *mysession);
struct httpclientsession *findsessiontoexpire(struct
http_server *server,int maxcount,int maxlife,int maxidle);
struct httpclientsession *findsession(struct request *req,int autocreate);
int connecttohttpd(char *host,int port,char *user,char *password,char *appname,int messagestostderr,int askpassword);
void errtosyslog(struct
http_server *server,char *s);
int servproc_init(struct
http_server *server,int human,int argc,char **argv);
signed char base64char(signed char c);
signed char char64value(signed char c);
void en64(char *dst,const char *src,long l);
unsigned long un64(char *s);
int urlencode(char *dst,const char *src,char except);
void unescape(char *s);
char *linefromtime(char *s,time_t t);
void makeid(char *dst);
int readline(int h,char *buffer,int length);
| Up - Design | Back - Protocol |