1
0

http server skeleton

This commit is contained in:
dtb 2022-05-14 20:47:15 -04:00
parent b6f5445e6f
commit 3e48f3d335
3 changed files with 117 additions and 0 deletions

3
src/http/config.h Normal file
View File

@ -0,0 +1,3 @@
#define Server "UNIX"
#define HTTP_ROOT "/var/www"
#define SERVER_ADDRESS "www.example.com"

94
src/http/url.c Normal file
View File

@ -0,0 +1,94 @@
#include "url.h"
/* safely frees URL struct */
struct Url *
free_url(struct Url *url){
free(url->s);
free(url->ssp);
free(url);
return NULL;
}
/* turns scheme specific part of URL from string into SSP struct */
/* returns NULL on allocation error or scheme syntax error */
struct CommonInternetScheme *
disassemble_commoninternetscheme(char *ssp){
int i;
char *p;
struct CommonInternetScheme *retval;
if( /* embarrassingly long if statement */
(retval
= (struct CommonInternetScheme *)
malloc(sizeof(struct CommonInternetScheme))
) == NULL
)
return NULL;
parsed = 0;
/* the scheme-specific data starts with a double slash to indicate that
* it complies with the common Internet scheme syntax (RFC 1738 3.1) */
if(ssp[0] != '/' || ssp[1] != '/')
return NULL;
ssp += 2;
/* probe to determine what areas of the SSP are filled */
p = ssp;
return retval;
}
/* turns URL string into URL struct */
struct Url *
disassemble_url(char *url){
char *p;
struct Url *retval;
size_t s_s;
size_t ssp_s;
size_t *s;
if((retval = (struct Url *)malloc(sizeof(struct Url))) == NULL)
return NULL;
s_s = 0;
ssp_s = 0;
/* get the lengths of the scheme and scheme specific part, excluding
* the nul byte */
p = url;
s = &s_s;
while(*p != '\0'){
/* standard; scheme names can't contain colons so the first
* colon must delimit the scheme from the scheme specific part
* (RFC 1738 2.1) */
if(s == &s_s && *p == ':'){
s = &ssp_s;
++p;
}
++*p;
}
/* malloc the lengths, including the nul byte */
if((retval->s = (char *)malloc(sizeof(char) * (s_s + 1))) == NULL)
goto free_retval;
if((retval->ssp = (char *)malloc(sizeof(char) * (ssp_s + 1))) == NULL)
goto free_s;
/* copy over the scheme and scheme specific part strings */
p = retval->s;
while(*url != ':')
*(p++) = *(url++);
*p = '\0';
p = retval->ssp;
while(*url != '\0')
*(p++) = *(url++);
*p = '\0';
return retval;
free_s:
free(retval->s);
free_retval:
free(retval);
return NULL;
}

20
src/http/url.h Normal file
View File

@ -0,0 +1,20 @@
/* #include <stdlib.h> /* free(3), malloc(3), NULL */
/* RFC 1738 3.1 */
struct CommonInternetScheme{
char *user;
char *password;
char *host; /* FQDN, IPv4, or IPv6 address */
char *port; /* Can have default; must be supplied in decimal. */
char *url_path;
};
/* standard; RFC 1738 */
struct Url{
char *s; /* scheme */
/* char * or some sort of struct SchemeSpecificPart * */
void *ssp; /* scheme-specific-part */
};
struct Url *disassemble_url(char *url);
struct Url *free_url(struct Url *url);