Пример #1
0
        public void receiveResponse(std::istream stream, HttpResponse response)
        {
            string httpVersion;

            readWord(stream, httpVersion);

            string status;
            char   c;

            stream.get(c);
            while (stream.good() && c != '\r')
            { //Till the end
                status += c;
                stream.get(c);
            }

            GlobalMembers.throwIfNotGood(stream);

            if (c == '\r')
            {
                stream.get(c);
                if (c != '\n')
                {
                    throw new System.Exception("Parser error: '\\n' symbol is expected");
                }
            }

            response.setStatus(parseResponseStatusFromString(status));

            string name;
            string value;

            while (readHeader(stream, name, value))
            {
                response.addHeader(name, value);
                name  = "";
                value = "";
            }

            response.addHeader(name, value);
            var  headers = response.getHeaders();
            uint length  = 0;
            var  it      = headers.find("content-length");

//C++ TO C# CONVERTER TODO TASK: Iterators are only converted within the context of 'while' and 'for' loops:
            if (it != headers.end())
            {
//C++ TO C# CONVERTER TODO TASK: Iterators are only converted within the context of 'while' and 'for' loops:
                length = Convert.ToUInt32(it.second);
            }

            string body;

            if (length != null)
            {
                readBody(stream, body, new uint(length));
            }

            response.setBody(body);
        }
Пример #2
0
        private void readBody(std::istream stream, string body, uint bodyLen)
        {
            uint read = 0;

            while (stream.good() && read < bodyLen)
            {
                body += stream.get();
                ++read;
            }

            GlobalMembers.throwIfNotGood(stream);
        }
Пример #3
0
        private void readWord(std::istream stream, string word)
        {
            char c;

            stream.get(c);
            while (stream.good() && c != ' ' && c != '\r')
            {
                word += c;
                stream.get(c);
            }

            GlobalMembers.throwIfNotGood(stream);

            if (c == '\r')
            {
                stream.get(c);
                if (c != '\n')
                {
                    throw std::system_error(GlobalMembers.make_error_code(CryptoNote.error.HttpParserErrorCodes.UNEXPECTED_SYMBOL));
                }
            }
        }
Пример #4
0
        private bool readHeader(std::istream stream, string name, string value)
        {
            char c;
            bool isName = true;

            stream.get(c);
            while (stream.good() && c != '\r')
            {
                if (c == ':')
                {
                    if (stream.peek() == ' ')
                    {
                        stream.get(c);
                    }

                    if (string.IsNullOrEmpty(name))
                    {
                        throw std::system_error(GlobalMembers.make_error_code(CryptoNote.error.HttpParserErrorCodes.EMPTY_HEADER));
                    }

                    if (isName)
                    {
                        isName = false;
                        stream.get(c);
                        continue;
                    }
                }

                if (isName)
                {
                    name += c;
                    stream.get(c);
                }
                else
                {
                    value += c;
                    stream.get(c);
                }
            }

            GlobalMembers.throwIfNotGood(stream);

            stream.get(c);
            if (c != '\n')
            {
                throw std::system_error(GlobalMembers.make_error_code(CryptoNote.error.HttpParserErrorCodes.UNEXPECTED_SYMBOL));
            }

            std::transform(name.GetEnumerator(), name.end(), name.GetEnumerator(), global::tolower);

            c = stream.peek();
            if (c == '\r')
            {
                stream.get(c).get(c);
                if (c != '\n')
                {
                    throw std::system_error(GlobalMembers.make_error_code(CryptoNote.error.HttpParserErrorCodes.UNEXPECTED_SYMBOL));
                }

                return(false); //no more headers
            }

            return(true);
        }