#include <malloc.h>
+#include <stdlib.h>
#include <string.h>
#include <time.h>
/* Content-Encoding */
if (encoding != NULL) {
- sprintf (tmp, "Content-Encoding: %sn", encoding);
+ sprintf (tmp, "Content-Encoding: %s", encoding);
add_line (buffer, tmp);
}
int processing (char *data, int len, conf_t *conf, char **pdata)
{
+ char *saved_data = data;
char location[BUFFER_SIZE] = {0};
VERBOSE (DEBUG, PRINT ("Start processing\n"));
/* check method */
- char *line = find_sequence (data, len, "\r\n", &data);
+ char *line = find_sequence (data, len + data - saved_data, "\r\n", &data);
if (line == NULL) {
VERBOSE (WARNING, PRINT ("Unknown received data\n"));
if (pdata != NULL) {
VERBOSE (INFO, PRINT ("%s %s (%s)\n", (type == get_e) ? "Get" : (type == head_e) ? "Head" : "Post", uri, version));
/* analyse uri */
- char *filename = (char *)calloc (strlen (conf->root) + strlen (uri) + 2, 1);
+ char *filename = strtok (uri, "&");
+ char *variables = strtok (NULL, "\n");
+ char *path = (char *) calloc (strlen (conf->root) + strlen (filename) + 2, 1);
//sprintf (filename, "%s%s%s", conf->root, ((conf->root[strlen (conf->root) - 1] != '/') && (uri[0] != '/')) ? "/" : "", uri);
- sprintf (filename, "%s/%s", conf->root, uri);
+ sprintf (path, "%s/%s", conf->root, filename);
/* check header */
header_t header = {0};
- while (strcmp (line = find_sequence (data, len, "\r\n", &data), "") != 0) {
+ while (strcmp (line = find_sequence (data, len + data - saved_data, "\r\n", &data), "") != 0) {
VERBOSE (DEBUG, PRINT ("Header line: '%s'\n", line));
char *field = strtok (line, ":");
char *value = trim (strtok (NULL, "\r"));
}
VERBOSE (DEBUG, PRINT ("Analyse field\n"));
- if (strcmp (field, "Allow") == 0) {
- header.allow = value;
- } else if (strcmp (field, "Authorization") == 0) {
- header.authorization = value;
- } else if (strcmp (field, "Content-Encoding") == 0) {
- header.content_encoding = value;
- } else if (strcmp (field, "Content-Length") == 0) {
- header.content_length = value;
- } else if (strcmp (field, "Content-Type") == 0) {
- header.content_type = value;
- } else if (strcmp (field, "Date") == 0) {
- header.date = value;
- } else if (strcmp (field, "Expires") == 0) {
- header.expires = value;
- } else if (strcmp (field, "From") == 0) {
- header.from = value;
- } else if (strcmp (field, "If-Modified-Since") == 0) {
- header.if_modified_since = value;
- } else if (strcmp (field, "Last-Modified") == 0) {
- header.last_modified = value;
- } else if (strcmp (field, "Location") == 0) {
- header.location = value;
- } else if (strcmp (field, "Pragma") == 0) {
- header.pragma = value;
- } else if (strcmp (field, "Referer") == 0) {
- header.referer = value;
- } else if (strcmp (field, "Server") == 0) {
- header.server = value;
- } else if (strcmp (field, "User-Agent") == 0) {
- header.user_agent = value;
- } else if (strcmp (field, "WWW-Authenticate") == 0) {
- header.www_authenticate = value;
- } else {
- VERBOSE (WARNING, PRINT ("Unknown header field: '%s'\n", field));
- }
+ if (strcmp (field, "Allow") == 0) { header.allow = value; }
+ else if (strcmp (field, "Authorization") == 0) { header.authorization = value; }
+ else if (strcmp (field, "Content-Encoding") == 0) { header.content_encoding = value; }
+ else if (strcmp (field, "Content-Length") == 0) { header.content_length = value; }
+ else if (strcmp (field, "Content-Type") == 0) { header.content_type = value; }
+ else if (strcmp (field, "Date") == 0) { header.date = value; }
+ else if (strcmp (field, "Expires") == 0) { header.expires = value; }
+ else if (strcmp (field, "From") == 0) { header.from = value; }
+ else if (strcmp (field, "If-Modified-Since") == 0) { header.if_modified_since = value; }
+ else if (strcmp (field, "Last-Modified") == 0) { header.last_modified = value; }
+ else if (strcmp (field, "Location") == 0) { header.location = value; }
+ else if (strcmp (field, "Pragma") == 0) { header.pragma = value; }
+ else if (strcmp (field, "Referer") == 0) { header.referer = value; }
+ else if (strcmp (field, "Server") == 0) { header.server = value; }
+ else if (strcmp (field, "User-Agent") == 0) { header.user_agent = value; }
+ else if (strcmp (field, "WWW-Authenticate") == 0) { header.www_authenticate = value; }
+ else { VERBOSE (WARNING, PRINT ("Unknown header field: '%s'\n", field)); }
}
VERBOSE (DEBUG, print_header_values (&header));
+ /* body */
+ char *body = data;
+ len -= saved_data - data;
+ if (len != (header.content_length ? atoi (header.content_length) : 0)) {
+ VERBOSE (WARNING, PRINT ("Incoherent size (%d <> %s)\n", len, header.content_length));
+ }
+
/* response */
char *buffer = NULL;
+ FILE *fid = NULL;
switch (type) {
case get_e:
- VERBOSE (DEBUG, PRINT ("Read file %s\n", filename));
- len = readfile (&buffer, filename);
- if (len == 0) {
+ case post_e: /* fall through */
+ VERBOSE (DEBUG, PRINT ("Read file %s\n", path));
+ len = readfile (&buffer, path);
+ if (len < 0) {
sprintf (location, "http://%s/", conf->servername);
len = error_404 (pdata, location);
} else {
- sprintf (location, "http://%s%s", conf->servername, filename);
+ sprintf (location, "http://%s%s", conf->servername, path);
len = generic_response (pdata, location, buffer, len);
free (buffer);
}
break;
case head_e:
- break;
- case post_e:
- VERBOSE (DEBUG, PRINT ("Write file %s\n", filename));
+ VERBOSE (DEBUG, PRINT ("Test file %s\n", path));
+ fid = fopen (path, "rb");
+ if (fid == NULL) {
+ sprintf (location, "http://%s/", conf->servername);
+ len = error_404 (pdata, location);
+ } else {
+ fclose (fid);
+ sprintf (location, "http://%s%s", conf->servername, path);
+ len = generic_response (pdata, location, NULL, 0);
+ }
break;
case not_supported_e:
break;
/* cleaning */
VERBOSE (DEBUG, PRINT ("Cleaning\n"));
- if (filename) {
- VERBOSE (DEBUG, PRINT ("Cleaning filename\n"));
- free (filename);
+ if (path) {
+ VERBOSE (DEBUG, PRINT ("Cleaning path\n"));
+ free (path);
}
return len;