Ejemplo n.º 1
0
void HttpClient::proxyConnect(PNetworkStream stream, ConstStrA host, ConstStrA authorization) {
	if (host.find(':') == naturalNull) {
		return proxyConnect(stream,StringA(host+ConstStrA(":443")),authorization);
	}
	PNetworkStream nstream = new BufferedNetworkStream(stream);

	HttpRequest req(stream.get(), host, mCONNECT, !useHTTP10);
	if (!authorization.empty()) {
		req.setHeader(fldProxyAuthorization,authorization);
	}
	req.closeOutput();

	class ResponseResult: public IHttpResponseCB {
	public:
		virtual void storeStatus(natural statusCode, ConstStrA statusMessage) {
			status = statusCode;
			statusMsg.append(statusMessage);
		}
		virtual void storeHeaderLine(ConstStrA, ConstStrA) {

		}

		natural status;
		AutoArray<char, SmallAlloc<32> > statusMsg;
	};

	ResponseResult res;

	HttpResponse resp(stream.get(),res,HttpResponse::readHeadersNow);
	if (status != 200)
		throw HttpStatusException(THISLOCATION,host,res.status, res.statusMsg);
}
Ejemplo n.º 2
0
static ConstStrA findInAllowedSet(ConstStrA field, ConstStrA allowedSet) {
	using namespace LightSpeed;
	for (ConstStrA::SplitIterator iter = allowedSet.split(',');iter.hasItems();) {
		ConstStrA value = iter.getNext();
		natural eq = value.find('=');
		if (eq == naturalNull) {
			if (value == field) return value;
		} else {
			if (value.head(eq) == field) {
				return value.offset(eq+1);
			}
		}
	}
	return ConstStrA();
}
Ejemplo n.º 3
0
LightSpeed::StringA dbOrderFromJSON(const LightSpeed::JSON::INode &nd) {
	using namespace LightSpeed;
	AutoArray<char, SmallAlloc<256> > buff;
	for (natural i = 0; i < nd.getEntryCount();i++) {
		ConstStrA fld = nd[i].getStringUtf8();
		if (fld.empty() || fld == ConstStrA('^') || fld.find('`') != naturalNull)
			  throw ErrorMessageException(THISLOCATION, "Unacceptable field name");
		if (i) buff.append(ConstStrA(", "));
		if (fld[0] == '^') {
			buff.add('`');buff.append(fld.offset(1));buff.add('`');
			buff.append(ConstStrA(" DESC"));
		} else {
			buff.add('`');buff.append(fld);buff.add('`');
			buff.append(ConstStrA(" ASC"));
		}
	}
	return ConstStrA(buff);
}
Ejemplo n.º 4
0
ITCPServerConnHandler::Command  HttpReqImpl::readHeader() {

	try {

		AutoArray<char, SmallAlloc<8192> > linebuff;

		NStream::Buffer &buffer = inout->getBuffer();
		natural fetched = buffer.fetch();
		if (fetched == 0) {
			errorPage(413,ConstStrA(),"Header is too large");
			return ITCPServerConnHandler::cmdRemove;
		}
		natural pos = buffer.lookup(ConstBin("\r\n"),fetched);
		while (pos != naturalNull) {

			linebuff.resize(pos+2);
			buffer.read(linebuff.data(),pos+2);
			ConstStrA line = linebuff.head(pos);

			if (method.empty()) {
				if (pos == 0) return ITCPServerConnHandler::cmdWaitRead;

				reqBeginTime = TimeStamp::now();
				reportDuration = true;

				cropWhite(line);
				ConstStrA::SplitIterator splt = line.split(' ');
				method = hdrPool.add(ConstStrA(splt.getNext()));
				path = hdrPool.add(ConstStrA(splt.getNext()));
				while (path.empty()) path = hdrPool.add(ConstStrA(splt.getNext()));
				protocol = hdrPool.add(ConstStrA(splt.getNext()));
				while (protocol.empty()) protocol = hdrPool.add(ConstStrA(splt.getNext()));
				//parse some fields
				TextParser<char,StaticAlloc<256> > parser;

				//check http version
				if (parser("HTTP/%u1.%u2",protocol)) {
						httpMajVer = (unsigned short)((natural)parser[1]);
						httpMinVer = (unsigned short)((natural)parser[2]);
						if (httpMajVer != 1 || (httpMinVer != 0 && httpMinVer != 1))
							return errorPageKA(505);
						useHTTP11(httpMinVer == 1);
				} else {
					return errorPageKA(400,StringA(ConstStrA("Unknown protocol: ")+ protocol));
				}
			} else if (line.empty()) {
				return finishReadHeader();
			} else {

				natural dblcolon = line.find(':');
				if (dblcolon == naturalNull) {
					errorPage(400,ConstStrA(),"line");
					return ITCPServerConnHandler::cmdRemove;
				}
				ConstStrA field = line.head(dblcolon);
				ConstStrA value = line.offset(dblcolon+1);
				cropWhite(field);
				cropWhite(value);
				requestHdrs.insert(hdrPool.add(field),hdrPool.add(value));
			}
			pos = buffer.lookup(ConstBin("\r\n"));
		}
		return ITCPServerConnHandler::cmdWaitRead;
	} catch (AllocatorLimitException &) {
		errorPage(413,ConstStrA(),"Header is too large (4096 bytes)");
		return ITCPServerConnHandler::cmdRemove;
	}
}