static void s_ReadSocket(CSocket& sock, void* buffer, size_t buffer_size, size_t* bytes_read) { EIO_Status status; while ((status = sock.Read(buffer, buffer_size, bytes_read)) == eIO_Interrupt) /* no-op */; if (status != eIO_Success) { // eIO_Timeout, eIO_Closed, eIO_InvalidArg, // eIO_NotSupported, eIO_Unknown NCBI_THROW_FMT(CNetStorageException, eIOError, "I/O error while reading from NetStorage server " << sock.GetPeerAddress() << ". " "Socket status: " << IO_StatusStr(status) << '.'); } }
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()); }