util.c 2.94 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1
#include <sys/types.h>
2 3
#include <stdio.h>
#include <string.h>
Jack Moffitt's avatar
Jack Moffitt committed
4 5

#ifndef _WIN32
6 7
#include <sys/time.h>
#include <sys/socket.h>
Jack Moffitt's avatar
Jack Moffitt committed
8
#include <unistd.h>
9 10 11
#ifdef HAVE_POLL
#include <sys/poll.h>
#endif
12
#else
13
#include <winsock2.h>
14
#include <windows.h>
15 16 17 18
#include <stdio.h>
#define snprintf _snprintf
#define strcasecmp stricmp
#define strncasecmp strnicmp
Jack Moffitt's avatar
Jack Moffitt committed
19 20 21 22 23 24
#endif

#include "sock.h"

#include "config.h"
#include "util.h"
25
#include "os.h"
Jack Moffitt's avatar
Jack Moffitt committed
26

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
/* Abstract out an interface to use either poll or select depending on which
 * is available (poll is preferred) to watch a single fd.
 *
 * timeout is in milliseconds.
 *
 * returns > 0 if activity on the fd occurs before the timeout.
 *           0 if no activity occurs
 *         < 0 for error.
 */
int util_timed_wait_for_fd(int fd, int timeout)
{
#ifdef HAVE_POLL
    struct pollfd ufds;

    ufds.fd = fd;
    ufds.events = POLLIN;
    ufds.revents = 0;

    return poll(&ufds, 1, timeout);
#else
    fd_set rfds;
    struct timeval tv;

    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);

    tv.tv_sec = timeout/1000;
    tv.tv_usec = (timeout - tv.tv_sec)*1000;

    return select(fd+1, &rfds, NULL, NULL, &tv);
#endif
}

Jack Moffitt's avatar
Jack Moffitt committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
int util_read_header(int sock, char *buff, unsigned long len)
{
	int read_bytes, ret;
	unsigned long pos;
	char c;
	ice_config_t *config;

	config = config_get_config();

	read_bytes = 1;
	pos = 0;
	ret = 0;

	while ((read_bytes == 1) && (pos < (len - 1))) {
		read_bytes = 0;

76
        if (util_timed_wait_for_fd(sock, config->header_timeout*1000) > 0) {
Jack Moffitt's avatar
Jack Moffitt committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

			if ((read_bytes = recv(sock, &c, 1, 0))) {
				if (c != '\r') buff[pos++] = c;
				if ((pos > 1) && (buff[pos - 1] == '\n' && buff[pos - 2] == '\n')) {
					ret = 1;
					break;
				}
			}
		} else {
			break;
		}
	}

	if (ret) buff[pos] = '\0';
	
	return ret;
}

95 96 97 98 99 100 101 102 103
int util_get_full_path(char *uri, char *fullPath, int fullPathLen) {
	int ret = 0;
	if (uri) {
		memset(fullPath, '\000', fullPathLen);
		snprintf(fullPath, fullPathLen-1, "%s%s%s", config_get_config()->webroot_dir, PATH_SEPARATOR, uri);
		ret = 1;
	}
	return ret;
}
Jack Moffitt's avatar
Jack Moffitt committed
104

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
int util_check_valid_extension(char *uri) {
	int	ret = 0;
	char	*p2;

	if (uri) {
		p2 = strrchr(uri, '.');
		if (p2) {
			p2++;
			if (strncmp(p2, "xsl", strlen("xsl")) == 0) {
				/* Build the full path for the request, concatenating the webroot from the config.
				** Here would be also a good time to prevent accesses like '../../../../etc/passwd' or somesuch.
				*/
				ret = XSLT_CONTENT;
			}
			if (strncmp(p2, "htm", strlen("htm")) == 0) {
				/* Build the full path for the request, concatenating the webroot from the config.
				** Here would be also a good time to prevent accesses like '../../../../etc/passwd' or somesuch.
				*/
				ret = HTML_CONTENT;
			}
			if (strncmp(p2, "html", strlen("html")) == 0) {
				/* Build the full path for the request, concatenating the webroot from the config.
				** Here would be also a good time to prevent accesses like '../../../../etc/passwd' or somesuch.
				*/
				ret = HTML_CONTENT;
			}

		}
	}
	return ret;
}
Jack Moffitt's avatar
Jack Moffitt committed
136 137