File: http_file_file.cpp

package info (click to toggle)
entropybroker 2.9-9
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,700 kB
  • sloc: cpp: 14,387; sh: 934; makefile: 188; java: 148; perl: 12
file content (105 lines) | stat: -rw-r--r-- 1,804 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <map>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <unistd.h>
#include <vector>

#include "log.h"
#include "statistics.h"
#include "statistics_global.h"
#include "statistics_user.h"
#include "http_bundle.h"
#include "http_request_t.h"
#include "http_file.h"
#include "http_file_file.h"

http_file_file::http_file_file(std::string url_in, std::string meta_in, std::string file_in) : url(url_in), meta(meta_in), file(file_in)
{
}

http_file_file::~http_file_file()
{
}

std::string http_file_file::get_url()
{
	return url;
}

std::string http_file_file::get_meta_type()
{
	return meta;
}

void http_file_file::load_file(unsigned char **p, int *len)
{
	bool ok = true;
	struct stat st;

	for(;;)
	{
		if (stat(file.c_str(), &st) == -1)
		{
			dolog(LOG_INFO, "stat on %s failed: %s", file.c_str(), strerror(errno));
			ok = false;
			break;
		}

		FILE *fh = fopen(file.c_str(), "rb");
		if (!fh)
		{
			ok = false;
			break;
		}

		*p = (unsigned char *)malloc(st.st_size);
		if (!*p)
		{
			ok = false;
			fclose(fh);
			break;
		}

		if (fread(*p, st.st_size, 1, fh) != 1)
		{
			dolog(LOG_INFO, "short read on %s", file.c_str());
			ok = false;
			fclose(fh);
			free(*p);
			break;
		}

		*len = st.st_size;

		fclose(fh);

		break;
	}

	if (!ok)
	{
		*p = (unsigned char *)strdup("file not found");
		*len = strlen((char *)*p);
	}
}

http_bundle * http_file_file::do_request(http_request_t request_type, std::string request_url, http_bundle *request_details)
{
	unsigned char *data = NULL;
	int data_len = 0;

	load_file(&data, &data_len);

	std::vector<std::string> reply_headers;

	http_bundle *result = new http_bundle(reply_headers, data, data_len);

	free(data);

	return result;
}