File: header.cc

package info (click to toggle)
apt-cacher-ng 0.7.27-1~bpo70+1
  • links: PTS
  • area: main
  • in suites: wheezy-backports
  • size: 1,740 kB
  • sloc: cpp: 13,987; sh: 519; perl: 442; ansic: 414; makefile: 77
file content (361 lines) | stat: -rw-r--r-- 6,597 bytes parent folder | download
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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361

//#define LOCAL_DEBUG
#include "debug.h"

#include "acfg.h"

#include "header.h"
#include "config.h"
#include <acbuf.h>

#include <cstdio>
#include <iostream>
#include <string.h>
#include <unistd.h>

#include "fileio.h"
#include "filereader.h"

#include <map>

using namespace MYSTD;

#if 1
struct eHeadPos2label
{
	header::eHeadPos pos;
	const char *str;
};

eHeadPos2label mapId2Headname[] =
{
		{ header::LAST_MODIFIED, "Last-Modified"},
		{ header::CONTENT_LENGTH, "Content-Length"},
		{ header::CONNECTION, "Connection"},
		{ header::CONTENT_TYPE, "Content-Type"},
		{ header::IF_MODIFIED_SINCE, "If-Modified-Since"},
		{ header::RANGE, "Range"},
		{ header::IFRANGE, "If-Range"},
		{ header::CONTENT_RANGE, "Content-Range"},
		{ header::PROXY_CONNECTION, "Proxy-Connection"},
		{ header::TRANSFER_ENCODING, "Transfer-Encoding"},
		{ header::AUTHORIZATION, "Authorization"},
		{ header::LOCATION, "Location" },
		{ header::XFORWARDEDFOR, "X-Forwarded-For"},
		{ header::XORIG, "X-Original-Source"}
};
#endif
#if 0 // nonsense... save a penny, waste an hour
struct tHeadLabelMap
{
	class noCaseComp
	{
	//	bool operator<(const tStringRef &a) { return strncasecmp(a.first, first, second)<0; }
	};
	map<pair<const char*,size_t>, header::eHeadPos> lookup;
	tHeadLabelMap()
	{
		//tHeadLabelMap &x=*this;
		insert(make_pair(tStringRef(NAMEWLEN("foo")), header::XORIG));
	}
} label_map;
#endif

header::header()
:
	type(INVALID),
	m_nEstimLength(0)
{
	memset(h, 0, sizeof(h));
}

header::header(const header &s)
:type(s.type),
 frontLine(s.frontLine),
 m_nEstimLength(s.m_nEstimLength)
{
	for (UINT i = 0; i < HEADPOS_MAX; i++)
		h[i] = s.h[i] ? strdup(s.h[i]) : NULL;
}

header& header::operator=(const header& s)
{
	type=s.type;
	frontLine=s.frontLine;
	m_nEstimLength=s.m_nEstimLength;
	for (UINT i = 0; i < HEADPOS_MAX; ++i)
	{
		if (h[i])
			free(h[i]);
		h[i] = s.h[i] ? strdup(s.h[i]) : NULL;
	}
	return *this;
}

header::~header()
{
	for(UINT i=0; i<HEADPOS_MAX; i++)
		free(h[i]);
}

void header::clear()
{
	for(UINT i=0; i<HEADPOS_MAX; i++)
		del((eHeadPos) i);
	frontLine.clear();
	type=INVALID;
	m_nEstimLength=0;
}

void header::del(eHeadPos i)
{
	free(h[i]);
	h[i]=0;
}

inline int header::Load(const char * const in, UINT maxlen)
{
	if(maxlen<9)
		return 0;

	if(!in)
		return -1;
	if(!strncmp(in,  "HTTP/1.", 7))
		type=ANSWER;
	else if(!strncmp(in, "GET ", 4))
		type=GET;
	else if (!strncmp(in, "HEAD ", 5))
		type=HEAD;
	else if (!strncmp(in, "POST ", 5))
		type=POST;
	else if (!strncmp(in, "CONNECT ", 8))
		type=CONNECT;
	else
		return -1;

	const char *posNext=in;

	while (true)
	{

		const char *szBegin=posNext;
		UINT pos=szBegin-in;
		const char *end=(const char*) memchr(szBegin, '\r', maxlen-pos);
		if (!end)
			return 0;
		if (end+1>=in+maxlen)
			return 0; // one newline must fit there, always

		if (szBegin==end)
		{
			if (end[1]=='\n')
			{
				m_nEstimLength=end+2-in;
				return m_nEstimLength; // end detected
			}

			return -1; // looks like crap
		}
		posNext=end+2;

		while (isspace((UINT)*end))	end--;
		end++;
		
		if (frontLine.empty())
		{
			frontLine.assign(in, end-in);
			trimBack(frontLine);
			continue;
		}

		// end is on the last relevant char now
		const char *sep=(const char*) memchr(szBegin, ':', end-szBegin);
		if (!sep)
			return -1;
		
		const char *key = szBegin;
		size_t keyLen=sep-szBegin;

		sep++;
		while (sep<end && isspace((UINT)*sep))
			sep++;
		
		for(const eHeadPos2label *pMap=mapId2Headname;
				pMap<mapId2Headname+_countof(mapId2Headname); ++pMap)
		{
			if (strncasecmp(pMap->str, key, keyLen))
				continue;
			UINT l=end-sep;
			if( ! (h[pMap->pos] = (char*) realloc(h[pMap->pos], l+1)))
				continue;
			memcpy(h[pMap->pos], sep, l);
			h[pMap->pos][l]='\0';
			break;
		}
	}
	return -2;
}

int header::LoadFromBuf(const char * const in, UINT maxlen)
{
	clear();
	int ret=Load(in, maxlen);
	if(ret<0)
		clear();
	return ret;
}

int header::LoadFromFile(const string &sPath)
{
	clear();
#if 0
	filereader buf;
	return buf.OpenFile(sPath, true) && LoadFromBuf(buf.GetBuffer(), buf.GetSize());
#endif
	acbuf buf;
	if(!buf.initFromFile(sPath.c_str()))
		return -1;
	return LoadFromBuf(buf.rptr(), buf.size());
//#endif
}


void header::set(eHeadPos i, const char *val)
{
	if (h[i])
	{
		free(h[i]);
		h[i]=NULL;
	}
	if(val)
		h[i] = strdup(val);
}

void header::set(eHeadPos i, const char *val, size_t len)
{
	if(!val)
	{
		free(h[i]);
		h[i]=NULL;
		return;
	}
	h[i] = (char*) realloc(h[i], len+1);
	if(h[i])
	{
		memcpy(h[i], val, len);
		h[i][len]='\0';
	}
}

void header::set(eHeadPos key, cmstring &value)
{
	string::size_type l=value.size()+1;
	h[key]=(char*) realloc(h[key], l);
	if(h[key])
		memcpy(h[key], value.c_str(), l);
}

void header::set(eHeadPos key, off_t nValue)
{	
	char buf[3*sizeof(off_t)];
	int len=sprintf(buf, OFF_T_FMT, nValue);
    set(key, buf, len);
}

#ifndef MINIBUILD

mstring header::ToString() const
{
	string s = frontLine;
	s+="\r\n";
	for(const eHeadPos2label *pMap=mapId2Headname;
			pMap<mapId2Headname+_countof(mapId2Headname); ++pMap)
	{
		if (h[pMap->pos])
		{
			s += pMap->str;
			s += ": ";
			s += h[pMap->pos];
			s += "\r\n";
		}
	}
	s+= "Date: ";
	s+= tCurrentTime();
	s+= "\r\n\r\n";
	return s;
}

int header::StoreToFile(cmstring &sPath) const
{
	int nByteCount(0);
	const char *szPath=sPath.c_str();
	int fd=open(szPath, O_WRONLY|O_CREAT|O_TRUNC, acfg::fileperms);
	if(fd<0)
	{
		fd=-errno;
		// maybe there is something in the way which can be removed?
		if(::unlink(szPath))
			return fd;

		fd=open(szPath, O_WRONLY|O_CREAT|O_TRUNC, acfg::fileperms);
		if(fd<0)
			return -errno;
	}
	
	string hstr=ToString();
	const char *p=hstr.c_str();
	nByteCount=hstr.length();
	
	for(string::size_type pos=0; pos<(UINT)nByteCount;)
	{
		int ret=write(fd, p+pos, nByteCount-pos);
		if(ret<0)
		{
			if(EAGAIN == errno || EINTR == errno)
				continue;
			if(EINTR == errno)
				continue;
			
			ret=errno;
			forceclose(fd);
			return -ret;
		}
		pos+=ret;
	}

	while(0!=close(fd))
	{
		if(errno != EINTR)
			return -errno;
	}
	
	return nByteCount;
}

#endif // MINIBUILD

MYSTD::string header::GenInfoHeaders()
{
	    string ret="Date: ";
	    ret+=tCurrentTime();
	    ret+="\r\nServer: Debian Apt-Cacher NG/"ACVERSION"\r\n";
	    return ret;
}

static const char* fmts[] =
{
		"%a, %d %b %Y %H:%M:%S GMT",
		"%A, %d-%b-%y %H:%M:%S GMT",
		"%a %b %d %H:%M:%S %Y"
};

bool header::ParseDate(const char *s, struct tm *tm)
{
	if(!s || !tm)
		return false;
	for(const char **p=fmts; p<fmts+_countof(fmts); ++p)
		if(::strptime(s, *p, tm))
			return true;

	return false;
}