예제 #1
0
파일: AI1.cpp 프로젝트: gmoromisato/Hexarc
CString ExecuteHTTPGet (const CString &sInput)
	{
	//	Parse the input

	char *pPos = sInput.GetParsePointer() + STR_HTTP_GET_PREFIX.GetLength();

	//	Parse the URL

	CString sHost;
	CString sPath;
	if (!urlParse(pPos, NULL, &sHost, &sPath))
		return CString("Invalid URL.");

	//	If no host, then local host

	if (sHost.IsEmpty())
		sHost = CString("localhost");

	//	Connect

	CSocket theSocket;
	if (!theSocket.Connect(sHost, 80))
		return strPattern("Unable to connect to: %s.", sHost);

	//	Compose a request

	CHTTPMessage Request;
	Request.InitRequest(CString("GET"), sPath);
	Request.AddHeader(HEADER_HOST, sHost);
	Request.AddHeader(HEADER_CONNECTION, CString("keep-alive"));
#ifdef DEBUG_REQUEST_FRAGMENT_X
	Request.AddHeader(HEADER_USER_AGENT, CString("AI1/1.0 (This is a test of the header parsing system in Hexarc. There is probably a bug in which splitting the header across packets will cause failure of the HTTP parsing engine.)"));
#else
	Request.AddHeader(HEADER_USER_AGENT, CString("AI1/1.0"));
#endif

	//	Send the request

	CBuffer Buffer(4096);
	Request.WriteToBuffer(Buffer);

#ifdef DEBUG_REQUEST_FRAGMENT
	int iTotalLen = Buffer.GetLength();
	int iSplit = 105;

	if (iSplit < iTotalLen)
		{
		printf("Split at %d bytes\n", iSplit);
		CString sPart(Buffer.GetPointer(), iSplit);
		printf("%s\n", (LPSTR)sPart);

		theSocket.Write(Buffer.GetPointer(), iSplit);
		::Sleep(10);
		theSocket.Write(Buffer.GetPointer() + iSplit, iTotalLen - iSplit);
		}
	else
		theSocket.Write(Buffer.GetPointer(), Buffer.GetLength());
#else
	theSocket.Write(Buffer.GetPointer(), Buffer.GetLength());
#endif

	//	Now read the response. We build up a buffer to hold it.

	CHTTPMessage Response;
	CBuffer ResponseBuff;

	//	Keep reading until we've got enough (or until the connection drops)

	while (!Response.IsMessageComplete())
		{
		CBuffer TempBuffer(8192);

		//	Read

		int iBytesRead = theSocket.Read(TempBuffer.GetPointer(), 8192);
		TempBuffer.SetLength(iBytesRead);

		//	If we're no making progress, then we're done

		if (iBytesRead == 0)
			return strPattern("Unable to read entire message.");

		//	Add to entire buffer

		ResponseBuff.Write(TempBuffer.GetPointer(), iBytesRead);

		//	Parse to see if we're done

		if (!Response.InitFromPartialBuffer(TempBuffer))
			return strPattern("Unable to parse HTTP message.");
		}

	//	Done

	theSocket.Disconnect();

	return CString(ResponseBuff.GetPointer(), ResponseBuff.GetLength());
	}
예제 #2
0
bool CCSVParser::ParseRow (TArray<CString> &Row, CString *retsError)

//	ParseRow
//
//	Parses a row

	{
	enum EStates
		{
		stateStart,
		stateSingleQuote,
		stateDoubleQuote,
		stateInPlainValue,
		stateEndOfValue,
		stateCR,
		stateLF,
		};

	Row.DeleteAll();

	//	Parse the BOM, if any

	if (m_iFormat == formatUnknown)
		m_iFormat = ParseBOM();

	//	Keep reading until we hit the end of the line.

	EStates iState = stateStart;
	CBuffer Value;
	while (true)
		{
		switch (iState)
			{
			case stateStart:
				{
				switch (GetCurChar())
					{
					case '\0':
						return true;

					case ' ':
					case '\t':
						break;

					case ',':
						Row.Insert(NULL_STR);
						break;

					case '\r':
						iState = stateCR;
						break;

					case '\n':
						iState = stateLF;
						break;

					case '\'':
						iState = stateSingleQuote;
						break;

					case '\"':
						iState = stateDoubleQuote;
						break;

					default:
						Value.WriteChar(GetCurChar());
						iState = stateInPlainValue;
						break;
					}
				break;
				}

			case stateSingleQuote:
				{
				switch (GetCurChar())
					{
					case '\0':
						Row.Insert(CString(Value.GetPointer(), Value.GetLength()));
						return true;

					case '\'':
						Row.Insert(CString(Value.GetPointer(), Value.GetLength()));
						Value.SetLength(0);
						iState = stateEndOfValue;
						break;

					default:
						Value.WriteChar(GetCurChar());
						break;
					}
				break;
				}

			case stateDoubleQuote:
				{
				switch (GetCurChar())
					{
					case '\0':
						Row.Insert(CString(Value.GetPointer(), Value.GetLength()));
						return true;

					case '\"':
						Row.Insert(CString(Value.GetPointer(), Value.GetLength()));
						Value.SetLength(0);
						iState = stateEndOfValue;
						break;

					default:
						Value.WriteChar(GetCurChar());
						break;
					}
				break;
				}

			case stateEndOfValue:
				{
				switch (GetCurChar())
					{
					case '\0':
						return true;

					case ' ':
					case '\t':
						break;

					case ',':
						iState = stateStart;
						break;

					case '\r':
						iState = stateCR;
						break;

					case '\n':
						iState = stateLF;
						break;

					default:
						break;
					}
				break;
				}

			case stateInPlainValue:
				{
				switch (GetCurChar())
					{
					case '\0':
						Row.Insert(CString(Value.GetPointer(), Value.GetLength()));
						return true;

					case ',':
						Row.Insert(CString(Value.GetPointer(), Value.GetLength()));
						Value.SetLength(0);
						iState = stateStart;
						break;

					case '\r':
						Row.Insert(CString(Value.GetPointer(), Value.GetLength()));
						Value.SetLength(0);
						iState = stateCR;
						break;

					case '\n':
						Row.Insert(CString(Value.GetPointer(), Value.GetLength()));
						Value.SetLength(0);
						iState = stateLF;
						break;

					default:
						Value.WriteChar(GetCurChar());
						break;
					}
				break;
				}

			case stateCR:
				{
				switch (GetCurChar())
					{
					case '\0':
						return true;

					case '\n':
						GetNextChar();
						return true;

					default:
						break;
					}
				break;
				}

			case stateLF:
				{
				switch (GetCurChar())
					{
					case '\0':
						return true;

					case '\r':
						GetNextChar();
						return true;

					default:
						return true;
					}
				break;
				}
			}

		GetNextChar();
		}
	}