示例#1
0
文件: http.c 项目: Abioy/sheepdog
static void *fetch_thread_run(void *arg)
{
	struct cache_handle *ch = (struct cache_handle *)arg;
	char url[PATH_MAX];
	char *pos = strstr(ch->path, PATH_HTTP) + strlen(PATH_HTTP);
	int ret;

	while (true) {
		sem_wait(&ch->prepare_sem);
		if (ch->stop)
			break;
		/* update cache */
		ret = generate_url(pos, strlen(ch->path) - strlen(PATH_HTTP),
				   url, PATH_MAX);
		if (ret)
			sheepfs_pr("failed to generate url for %s", ch->path);
		else {
			ret = curl_read_object(url, ch->prepare->mem,
					       CACHE_SIZE, ch->fetch_offset);
			ch->prepare->offset = ch->fetch_offset;
			ch->prepare->size = ret;
		}
		sem_post(&ch->ready_sem);
	}
	return NULL;
}
示例#2
0
void *thread_connection(void *args)
{
    int connection_socket = ((struct thread_arguments *) args ) -> connection_socket;
    struct sockaddr_in client_address = ((struct thread_arguments *) args ) -> client_address;

    struct client_data data = get_client_address(client_address);

    char buffer[BUFSIZE];
    bzero(buffer, BUFSIZE);
    int status = recv(connection_socket, buffer, BUFSIZE, MSG_DONTWAIT);

    if (WHITELIST != NULL && check_whitelist(data.ip_address) == NULL)
    {
        display_info(data, NULL, "Rejected connection from unknown user.");
        save_log(NULL, data.ip_address, data.hostname);
        if (write(connection_socket, "You are not whitelisted!\n", 26) < 0)
          printf("Error writing on stream socket\n");
        close(connection_socket);
        pthread_exit(NULL);
    }

    if (BANLIST != NULL && check_banlist(data.ip_address) != NULL)
    {
        display_info(data, NULL, "Rejected connection from banned user.");
        save_log(NULL, data.ip_address, data.hostname);
        if (write(connection_socket, "You are banned!\n", 17) < 0)
          printf("Error writing on stream socket\n");
        close(connection_socket);
        pthread_exit(NULL);
    }

    if (check_protocol(buffer) == 1)
        status = -1;

    if (status != -1)
    {
        char slug[SLUG_SIZE+8];
        generate_url(buffer, slug, SLUG_SIZE+8, data);
        save_log(slug, data.ip_address, data.hostname);
        char response[strlen(slug) + strlen(DOMAIN) + 2];
        snprintf(response, sizeof response, "%s%s\n", DOMAIN, slug);
        if (write(connection_socket, response, strlen(response)) < 0)
          printf("Error writing on stream socket\n");
    }
    else
    {
        display_info(data, NULL, "Invalid connection.");
        save_log(NULL, data.ip_address, data.hostname);
        if (write(connection_socket, "Use netcat.\n", 12) < 0)
          printf("Error writing on stream socket\n");
    }

    close(connection_socket);
    pthread_exit(NULL);
}
示例#3
0
文件: http.c 项目: Abioy/sheepdog
int http_object_write(const char *path, const char *buf, size_t size,
		      off_t ignore)
{
	char entry[PATH_MAX], url[PATH_MAX];
	int ret = -EINVAL;

	/* don't need '\n' at the end of 'buf' */
	ret = generate_url(buf, size - 1, url, PATH_MAX);
	if (ret)
		goto out;

	if (curl_object_exists(url)) {
		snprintf(entry, size, "%s", buf);
		ret = object_create_entry(entry, url);
		if (ret >= 0)
			ret = size;
	}
out:
	return ret;
}