Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Http::Response::getField not returning all data  (Read 2360 times)

0 Members and 1 Guest are viewing this topic.

Rajan

  • Newbie
  • *
  • Posts: 3
    • View Profile
sf::Http::Response::getField not returning all data
« on: August 26, 2013, 04:42:22 am »
Hi all i just installed sfml yesterday and im completely loving it!  My only issue is that when i call getField("Set-Cookie") not all the data is returned,  only 2 of 4-6 cookies are printed.  When I inspect the Set-Cookie after doing the same request w/ C# WebClient its much more populated.

sfml code
std::string GetCookie(std::string username,std::string pass)
{
        sf::Http http("http://www.nexon.net/");

        sf::Http::Request request("/api/v001/account/login",sf::Http::Request::Post);
        request.setHttpVersion(1,1);
        request.setField("Content-Type", "aanpplication/x-www-form-urlencoded");
        request.setField("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

        std::ostringstream stream;
        stream << "userID=" << username << "&password=" << pass;
        request.setBody(stream.str());

        sf::Http::Response response = http.sendRequest(request);

        if (response.getStatus() == sf::Http::Response::Ok)
        {
                prtinf("%s\n",response.getField("Set-Cookie").c_str());
                return response.GetField("Set-Cookie");
        }
        else
        {
                Log("HttpResponse is not sf::Http::Response::Ok\n");
        }

        return std::string("error");
}
 

c# code
static string GetAuth(string username , string password , IWebProxy proxy = null)
        {
            WebClient wc = new WebClient();
            wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
            wc.Proxy = proxy; //if proxy isnt null it will try to use the default one
 
            string rtn = "@error";
 
            try
            {
                wc.UploadString("http://www.nexon.net/api/v001/account/login",
                    string.Format("userID={0}&password={1}", username , password));
 
                rtn = wc.ResponseHeaders["Set-Cookie"];
            }
            catch (Exception) { } //exception handler
            finally //dipose of resource
            {
                if (wc != null)
                    wc.Dispose();
            }
 
            return rtn;
        }
 

Does anyone know why this is?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Http::Response::getField not returning all data
« Reply #1 on: August 26, 2013, 07:40:48 am »
If there are several "Set-Cookie" lines in the response, SFML only returns one.
Laurent Gomila - SFML developer

Rajan

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: sf::Http::Response::getField not returning all data
« Reply #2 on: August 26, 2013, 10:07:49 am »
Ahh I see,  damn..  Any chance support could be added for this I'm the future?  Also thank you for the quick reply :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Http::Response::getField not returning all data
« Reply #3 on: August 26, 2013, 10:39:10 am »
Quote
Any chance support could be added for this I'm the future?
Maybe :P
Laurent Gomila - SFML developer

Rajan

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: sf::Http::Response::getField not returning all data
« Reply #4 on: August 27, 2013, 04:32:54 am »
Hi i ended up creating my own Http class based off of the code on git for this,  its not 100% sanitary but it gets the job done

code is anyone cares
#include "Auth.h"
#include "Log.h"
#include <sstream>
#include <SFML/Network.hpp>


class HttpPostRequest
{
public:
        static const int MajorHttpVersion = 1;
        static const int MinorHttpVersion = 0;


        HttpPostRequest(std::string host, std::string uri)
        {
                m_host = host;
                m_uri = uri;
        }

        void AddHeader(std::string field,std::string value)
        {
                std::ostringstream out;
                out << field << ": " << value;
                m_headers.push_back(out.str());
        }

        void SetBody(std::string body)
        {
                m_body = body;
        }

        std::string GetResponse()
        {
                return m_response;
        }

        bool Send()
        {      
                sf::TcpSocket socket;

                if(socket.connect(m_host,80,sf::seconds(10)) == sf::Socket::Done)
                {
                        std::string data = Prepare();

                        if(socket.send(data.c_str(),data.size()) == sf::Socket::Done)
                        {
                                std::string receivedStr;
                                std::size_t size = 0;
                                char buffer[1024];

                                while (socket.receive(buffer, sizeof(buffer), size) == sf::Socket::Done)
                                {
                                        receivedStr.append(buffer, buffer + size);
                                }

                                m_response = receivedStr;
                        }

                        socket.disconnect();
                        return true;
                }

                return false;
        }

private:       
        std::string Prepare()
        {
                std::ostringstream out;


                // Write the first line containing the request type
                out << "POST " << m_uri << " HTTP/" << MajorHttpVersion << "." << MinorHttpVersion << "\r\n";

                // Write header
                for(auto& header : m_headers)
                {
                        out << header << "\r\n";
                }

                // Use an extra \r\n to separate the header from the body
                out << "Content-Length: " << m_body.size() << "\r\n\r\n" << m_body;    

                return out.str();
        }

        std::string m_host, m_uri, m_body, m_response;
        std::vector<std::string> m_headers;
};

std::string GetCookie(std::string username,std::string pass)
{
        std::string host = "nexon.net";

        HttpPostRequest request(host,"/api/v001/account/login");
        request.AddHeader("Content-Type","application/x-www-form-urlencoded");
        request.AddHeader("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
        request.AddHeader("Host",host);

        std::ostringstream stream;
        stream << "userID=" << username << "&password=" << pass;
        request.SetBody(stream.str());

        if(request.Send())
        {
                std::string result = request.GetResponse();
                std::string::size_type pos = result.find("NPPv2=");

                if (pos != std::string::npos)
        {
                        result.erase(0,pos + 6);
                        return result.substr(0,result.find(";"));
                }
                else
                {
                        Log("No NPPv2 in response\n");
                        return "error";
                }
        }
        else
        {
                Log("HttpPostRequest failed\n");
                return "error";
        }
}