// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
-#ifdef __GNUG__
+#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
#pragma implementation "http.h"
#endif
#include <stdio.h>
#include <stdlib.h>
+
+#ifndef WX_PRECOMP
#include "wx/string.h"
+#include "wx/app.h"
+#endif
+
#include "wx/tokenzr.h"
#include "wx/socket.h"
#include "wx/protocol/protocol.h"
#include "wx/sckstrm.h"
IMPLEMENT_DYNAMIC_CLASS(wxHTTP, wxProtocol)
-IMPLEMENT_PROTOCOL(wxHTTP, wxT("http"), wxT("80"), TRUE)
+IMPLEMENT_PROTOCOL(wxHTTP, wxT("http"), wxT("80"), true)
#define HTTP_BSIZE 2048
: wxProtocol()
{
m_addr = NULL;
- m_read = FALSE;
- m_proxy_mode = FALSE;
+ m_read = false;
+ m_proxy_mode = false;
m_post_buf = wxEmptyString;
+ m_http_response = 0;
SetNotify(wxSOCKET_LOST_FLAG);
}
m_proxy_mode = on;
}
+wxHTTP::wxHeaderIterator wxHTTP::FindHeader(const wxString& header)
+{
+ wxHeaderIterator it = m_headers.begin();
+ for ( wxHeaderIterator en = m_headers.end(); it != en; ++it )
+ {
+ if ( wxStricmp(it->first, header) == 0 )
+ break;
+ }
+
+ return it;
+}
+
+wxHTTP::wxHeaderConstIterator wxHTTP::FindHeader(const wxString& header) const
+{
+ wxHeaderConstIterator it = m_headers.begin();
+ for ( wxHeaderConstIterator en = m_headers.end(); it != en; ++it )
+ {
+ if ( wxStricmp(it->first, header) == 0 )
+ break;
+ }
+
+ return it;
+}
+
void wxHTTP::SetHeader(const wxString& header, const wxString& h_data)
{
if (m_read) {
ClearHeaders();
- m_read = FALSE;
+ m_read = false;
}
- wxStringToStringHashMap::iterator it = m_headers.find(header);
+ wxHeaderIterator it = FindHeader(header);
if (it != m_headers.end())
- it->second = h_data;
+ it->second = h_data;
else
- m_headers[header.Upper()] = h_data;
+ m_headers[header] = h_data;
}
-wxString wxHTTP::GetHeader(const wxString& header)
+wxString wxHTTP::GetHeader(const wxString& header) const
{
- wxStringToStringHashMap::iterator it = m_headers.find(header.Upper());
-
- if (it == m_headers.end())
- return wxEmptyString;
+ wxHeaderConstIterator it = FindHeader(header);
- return it->second;
+ return it == m_headers.end() ? wxGetEmptyString() : it->second;
}
void wxHTTP::SetPostBuffer(const wxString& post_buf)
{
- m_post_buf = post_buf;
+ m_post_buf = post_buf;
}
void wxHTTP::SendHeaders()
wxStringTokenizer tokenzr;
ClearHeaders();
- m_read = TRUE;
+ m_read = true;
#if defined(__VISAGECPP__)
// VA just can't stand while(1)
- bool bOs2var = TRUE;
- while(bOs2var) {
+ bool bOs2var = true;
+ while(bOs2var)
#else
- while (1) {
+ while (1)
#endif
+ {
m_perr = GetLine(this, line);
if (m_perr != wxPROTO_NOERR)
- return FALSE;
+ return false;
if (line.Length() == 0)
break;
wxString left_str = line.BeforeFirst(':');
- left_str.MakeUpper();
-
m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both);
}
- return TRUE;
+ return true;
}
bool wxHTTP::Connect(const wxString& host, unsigned short port)
delete m_addr;
m_addr = NULL;
m_perr = wxPROTO_NETERR;
- return FALSE;
+ return false;
}
if ( port ) addr->Service(port);
SetHeader(wxT("Host"), host);
- return TRUE;
+ return true;
}
bool wxHTTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait))
if (ipv4addr)
SetHeader(wxT("Host"), ipv4addr->OrigHostname());
- return TRUE;
+ return true;
}
bool wxHTTP::BuildRequest(const wxString& path, wxHTTP_Req req)
request = wxT("GET");
break;
case wxHTTP_POST:
- tmp_buf = wxT("POST");
+ request = wxT("POST");
+ if ( GetHeader( wxT("Content-Length") ).IsNull() )
+ SetHeader( wxT("Content-Length"), wxString::Format( wxT("%lu"), (unsigned long)m_post_buf.Len() ) );
break;
default:
- return FALSE;
+ return false;
}
+ m_http_response = 0;
+
// If there is no User-Agent defined, define it.
if (GetHeader(wxT("User-Agent")).IsNull())
- SetHeader(wxT("User-Agent"), wxT("wxWindows 2.x"));
+ SetHeader(wxT("User-Agent"), wxT("wxWidgets 2.x"));
SaveState();
- SetFlags(wxSOCKET_NONE);
- Notify(FALSE);
+
+ // we may use non blocking sockets only if we can dispatch events from them
+ SetFlags( wxIsMainThread() && wxApp::IsMainLoopRunning() ? wxSOCKET_NONE
+ : wxSOCKET_BLOCK );
+ Notify(false);
wxString buf;
buf.Printf(wxT("%s %s HTTP/1.0\r\n"), request, path.c_str());
Write("\r\n", 2);
if ( req == wxHTTP_POST ) {
- Write(m_post_buf, m_post_buf.Len());
+ Write(m_post_buf.mbc_str(), m_post_buf.Len());
m_post_buf = wxEmptyString;
}
m_perr = GetLine(this, tmp_str);
if (m_perr != wxPROTO_NOERR) {
RestoreState();
- return FALSE;
+ return false;
}
if (!tmp_str.Contains(wxT("HTTP/"))) {
SetHeader(wxT("Content-Length"), wxT("-1"));
SetHeader(wxT("Content-Type"), wxT("none/none"));
RestoreState();
- return TRUE;
+ return true;
}
wxStringTokenizer token(tmp_str,wxT(' '));
token.NextToken();
tmp_str2 = token.NextToken();
+ m_http_response = wxAtoi(tmp_str2);
+
switch (tmp_str2[0u]) {
case wxT('1'):
/* INFORMATION / SUCCESS */
default:
m_perr = wxPROTO_NOFILE;
RestoreState();
- return FALSE;
+ return false;
}
ret_value = ParseHeaders();
if (!m_addr)
return NULL;
- // We set m_connected back to FALSE so wxSocketBase will know what to do.
+ // We set m_connected back to false so wxSocketBase will know what to do.
#ifdef __WXMAC__
- wxSocketClient::Connect(*m_addr , FALSE );
+ wxSocketClient::Connect(*m_addr , false );
wxSocketClient::WaitOnConnect(10);
if (!wxSocketClient::IsConnected())
inp_stream->m_read_bytes = 0;
- Notify(FALSE);
+ Notify(false);
SetFlags(wxSOCKET_BLOCK | wxSOCKET_WAITALL);
return inp_stream;