- wxChar *tmp_buf;
- wxChar buf[200]; // 200 is arbitrary.
- wxString tmp_str = path;
-
- // If there is no User-Agent defined, define it.
- if (GetHeader(_T("User-Agent")).IsNull())
- SetHeader(_T("User-Agent"), _T("wxWindows 2.x"));
-
- switch (req) {
- case wxHTTP_GET:
- tmp_buf = _T("GET");
- break;
- default:
- return FALSE;
- }
-
- SaveState();
- SetFlags(NONE);
- Notify(FALSE);
-
- wxSprintf(buf, _T("%s %s HTTP/1.0\n\r"), tmp_buf, tmp_str.GetData());
- const wxWX2MBbuf pathbuf = wxConvLibc.cWX2MB(buf);
- Write(pathbuf, strlen(MBSTRINGCAST pathbuf));
- SendHeaders();
- Write("\n\r", 2);
-
- m_perr = GetLine(this, tmp_str);
- if (m_perr != wxPROTO_NOERR) {
- RestoreState();
- return FALSE;
- }
-
- if (!tmp_str.Contains(_T("HTTP/"))) {
- // TODO: support HTTP v0.9 which can have no header.
- // FIXME: tmp_str is not put back in the in-queue of the socket.
- SetHeader(_T("Content-Length"), _T("-1"));
- SetHeader(_T("Content-Type"), _T("none/none"));
- RestoreState();
- return TRUE;
- }
-
- wxStringTokenizer token(tmp_str,_T(' '));
- wxString tmp_str2;
- bool ret_value;
-
- token.NextToken();
- tmp_str2 = token.NextToken();
-
- switch (tmp_str2[(unsigned int) 0]) {
- case _T('1'):
- /* INFORMATION / SUCCESS */
- break;
- case _T('2'):
- /* SUCCESS */
- break;
- case _T('3'):
- /* REDIRECTION */
- break;
- default:
- m_perr = wxPROTO_NOFILE;
+ const wxChar *request;
+
+ switch (req)
+ {
+ case wxHTTP_GET:
+ request = wxT("GET");
+ break;
+
+ case wxHTTP_POST:
+ request = wxT("POST");
+ // Content length must be correct, so always set, possibly
+ // overriding the value set explicitly by a previous call to
+ // SetHeader("Content-Length").
+ if ( !m_postBuffer.IsEmpty() )
+ {
+ wxString len;
+ len << m_postBuffer.GetDataLen();
+
+ SetHeader(wxS("Content-Length"), len);
+ }
+
+ // However if the user had explicitly set the content type, don't
+ // override it with the content type passed to SetPostText().
+ if ( !m_contentType.empty() && GetContentType().empty() )
+ SetHeader(wxS("Content-Type"), m_contentType);
+ break;
+
+ default:
+ return false;
+ }
+
+ m_http_response = 0;
+
+ // If there is no User-Agent defined, define it.
+ if ( GetHeader(wxT("User-Agent")).empty() )
+ SetHeader(wxT("User-Agent"), wxT("wxWidgets 2.x"));
+
+ // Send authentication information
+ if (!m_username.empty() || !m_password.empty()) {
+ SetHeader(wxT("Authorization"), GenerateAuthString(m_username, m_password));
+ }
+
+ SaveState();
+
+ // we may use non blocking sockets only if we can dispatch events from them
+ int flags = wxIsMainThread() && wxApp::IsMainLoopRunning() ? wxSOCKET_NONE
+ : wxSOCKET_BLOCK;
+ // and we must use wxSOCKET_WAITALL to ensure that all data is sent
+ flags |= wxSOCKET_WAITALL;
+ SetFlags(flags);
+ Notify(false);
+
+ wxString buf;
+ buf.Printf(wxT("%s %s HTTP/1.0\r\n"), request, path.c_str());
+ const wxWX2MBbuf pathbuf = buf.mb_str();
+ Write(pathbuf, strlen(pathbuf));
+ SendHeaders();
+ Write("\r\n", 2);
+
+ if ( req == wxHTTP_POST ) {
+ if ( !m_postBuffer.IsEmpty() )
+ Write(m_postBuffer.GetData(), m_postBuffer.GetDataLen());
+
+ m_postBuffer.Clear();
+ }
+
+ wxString tmp_str;
+ m_lastError = ReadLine(this, tmp_str);
+ if (m_lastError != wxPROTO_NOERR) {
+ RestoreState();
+ return false;
+ }
+
+ if (!tmp_str.Contains(wxT("HTTP/"))) {
+ // TODO: support HTTP v0.9 which can have no header.
+ // FIXME: tmp_str is not put back in the in-queue of the socket.
+ m_lastError = wxPROTO_NOERR;
+ SetHeader(wxT("Content-Length"), wxT("-1"));
+ SetHeader(wxT("Content-Type"), wxT("none/none"));
+ RestoreState();
+ return true;
+ }
+
+ wxStringTokenizer token(tmp_str,wxT(' '));
+ wxString tmp_str2;
+ bool ret_value;
+
+ token.NextToken();
+ tmp_str2 = token.NextToken();
+
+ m_http_response = wxAtoi(tmp_str2);
+
+ switch ( tmp_str2[0u].GetValue() )
+ {
+ case wxT('1'):
+ /* INFORMATION / SUCCESS */
+ break;
+
+ case wxT('2'):
+ /* SUCCESS */
+ break;
+
+ case wxT('3'):
+ /* REDIRECTION */
+ break;
+
+ default:
+ m_lastError = wxPROTO_NOFILE;
+ RestoreState();
+ return false;
+ }
+
+ m_lastError = wxPROTO_NOERR;
+ ret_value = ParseHeaders();