void HttpGetSocket::OnConnect() { SetMethod( "GET" ); AddResponseHeader( "Accept", "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,video/x-mng,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1"); AddResponseHeader( "Accept-Language", "en-us,en;q=0.5"); AddResponseHeader( "Accept-Encoding", "gzip,deflate"); AddResponseHeader( "Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"); AddResponseHeader( "User-agent", MyUseragent() ); if (GetUrlPort() != 80 && GetUrlPort() != 443) AddResponseHeader( "Host", GetUrlHost() + ":" + Utility::l2string(GetUrlPort()) ); else AddResponseHeader( "Host", GetUrlHost() ); SendRequest(); }
void HttpClientSocket::Url(const std::string& url_in,std::string& host,port_t& port) { std::string url; url_this(url_in, m_protocol, m_host, m_port, url, m_url_filename); SetUrl(url); host = GetUrlHost(); port = GetUrlPort(); }
HttpGetSocket::HttpGetSocket(ISocketHandler& h,const std::string& url_in,const std::string& to_file) : HttpClientSocket(h, url_in) { if (to_file.size()) { SetFilename(to_file); } if (!Open(GetUrlHost(),GetUrlPort())) { if (!Connecting()) { Handler().LogError(this, "HttpGetSocket", -1, "connect() failed miserably", LOG_LEVEL_FATAL); SetCloseAndDelete(); } } }
void HttpPostSocket::OnConnect() { if (m_bMultipart) { DoMultipartPost(); } else { std::string body; // only fields, no files, add urlencoding for (std::map<std::string,std::list<std::string> >::iterator it = m_fields.begin(); it != m_fields.end(); it++) { std::string name = (*it).first; std::list<std::string>& ref = (*it).second; if (body.size()) { body += '&'; } body += name + "="; bool first = true; for (std::list<std::string>::iterator it = ref.begin(); it != ref.end(); it++) { std::string value = *it; if (!first) { body += "%0d%0a"; // CRLF } body += Utility::rfc1738_encode(value); first = false; } } // build header, send body SetMethod("POST"); SetHttpVersion( "HTTP/1.1" ); AddResponseHeader( "Host", GetUrlHost() ); // oops - this is actually a request header that we're adding.. AddResponseHeader( "User-agent", MyUseragent()); AddResponseHeader( "Accept", "text/html, text/plain, */*;q=0.01" ); AddResponseHeader( "Connection", "close" ); AddResponseHeader( "Content-type", "application/x-www-form-urlencoded" ); AddResponseHeader( "Content-length", Utility::l2string((long)body.size()) ); SendRequest(); // send body Send( body ); } }
void HttpPutSocket::OnConnect() { SetMethod( "PUT" ); SetHttpVersion( "HTTP/1.1" ); AddResponseHeader( "Host", GetUrlHost() ); AddResponseHeader( "Content-type", m_content_type ); AddResponseHeader( "Content-length", Utility::l2string(m_content_length) ); AddResponseHeader( "User-agent", MyUseragent() ); SendRequest(); FILE *fil = fopen(m_filename.c_str(), "rb"); if (fil) { size_t n; char buf[32768]; while ((n = fread(buf, 1, 32768, fil)) > 0) { SendBuf(buf, n); } fclose(fil); } }
void HttpPutSocket::OnConnect() { SetMethod( "PUT" ); SetHttpVersion( "HTTP/1.1" ); AddResponseHeader( "Host", GetUrlHost() ); AddResponseHeader( "Content-type", m_content_type ); AddResponseHeader( "Content-length", Utility::l2string(m_content_length) ); AddResponseHeader( "User-agent", MyUseragent() ); SendRequest(); std::auto_ptr<IFile> fil = std::auto_ptr<IFile>(new File); if (fil -> fopen(m_filename, "rb")) { size_t n; char buf[32768]; while ((n = fil -> fread(buf, 1, 32768)) > 0) { SendBuf(buf, n); } fil -> fclose(); } }
void HttpPostSocket::DoMultipartPost() { long length = 0; // calculate content_length of our post body std::string tmp; // fields { for (std::map<std::string,std::list<std::string> >::iterator it = m_fields.begin(); it != m_fields.end(); it++) { std::string name = (*it).first; std::list<std::string>& ref = (*it).second; tmp = "--" + m_boundary + "\r\n" "content-disposition: form-data; name=\"" + name + "\"\r\n" "\r\n"; for (std::list<std::string>::iterator it = ref.begin(); it != ref.end(); it++) { std::string value = *it; tmp += value + "\r\n"; } length += (long)tmp.size(); } } // files { for (std::map<std::string,std::string>::iterator it = m_files.begin(); it != m_files.end(); it++) { std::string name = (*it).first; std::string filename = (*it).second; long content_length = m_content_length[filename]; std::string content_type = m_content_type[filename]; tmp = "--" + m_boundary + "\r\n" "content-disposition: form-data; name=\"" + name + "\"; filename=\"" + filename + "\"\r\n" "content-type: " + content_type + "\r\n" "\r\n"; length += (long)tmp.size(); length += content_length; length += 2; // crlf after file } } // end tmp = "--" + m_boundary + "--\r\n"; length += (long)tmp.size(); // build header, send body SetMethod("POST"); SetHttpVersion( "HTTP/1.1" ); AddResponseHeader( "Host", GetUrlHost() ); // oops - this is actually a request header that we're adding.. AddResponseHeader( "User-agent", MyUseragent()); AddResponseHeader( "Accept", "text/html, text/plain, */*;q=0.01" ); AddResponseHeader( "Connection", "close" ); AddResponseHeader( "Content-type", "multipart/form-data; boundary=" + m_boundary ); AddResponseHeader( "Content-length", Utility::l2string(length) ); SendRequest(); // send fields { for (std::map<std::string,std::list<std::string> >::iterator it = m_fields.begin(); it != m_fields.end(); it++) { std::string name = (*it).first; std::list<std::string>& ref = (*it).second; tmp = "--" + m_boundary + "\r\n" "content-disposition: form-data; name=\"" + name + "\"\r\n" "\r\n"; for (std::list<std::string>::iterator it = ref.begin(); it != ref.end(); it++) { std::string value = *it; tmp += value + "\r\n"; } Send( tmp ); } } // send files { for (std::map<std::string,std::string>::iterator it = m_files.begin(); it != m_files.end(); it++) { std::string name = (*it).first; std::string filename = (*it).second; std::string content_type = m_content_type[filename]; tmp = "--" + m_boundary + "\r\n" "content-disposition: form-data; name=\"" + name + "\"; filename=\"" + filename + "\"\r\n" "content-type: " + content_type + "\r\n" "\r\n"; Send( tmp ); { FILE *fil = fopen(filename.c_str(),"rb"); if (fil) { char slask[2000]; // for fread size_t n; while ((n = fread(slask, 1, 2000, fil)) > 0) { SendBuf(slask, n); } fclose(fil); } } Send("\r\n"); } } // end of send Send("--" + m_boundary + "--\r\n"); }
void HttpPostSocket::Open() { // why do I have to specify TcpSocket:: to get to the Open() method?? TcpSocket::Open(GetUrlHost(), GetUrlPort()); }
void HttpGetSocket::DoConnect() { DoConnect(GetUrlHost(), GetUrlPort()); }