1 //
2 // C++ Implementation: http
3 //
4 // Description:This file contains the implementation of the mHTTP class.
5 //
6 //
7 // Author: Max Magalhães Velasques <max@debiancomp1>, (C) 2006
8 //
9 // Copyright: See COPYING file that comes with this distribution
10 //
11 //
12
13 #include "wxDFast.h"
14
mHTTP()15 mHTTP::mHTTP():wxHTTP()
16 {
17 m_getcommand = wxEmptyString;
18 m_headersmsg = wxEmptyString;
19 m_messagereceived = wxEmptyString;
20 m_use_proxy = false;
21 }
22
Connect(wxSockAddress & addr,bool wait)23 bool mHTTP::Connect(wxSockAddress& addr, bool wait)
24 {
25 return (wxProtocol::Connect(addr,wait));
26 }
27
UseProxy(wxString proxy_authstring)28 void mHTTP::UseProxy(wxString proxy_authstring)
29 {
30 m_use_proxy = true;
31 m_proxy_authstring = proxy_authstring;
32 }
33
BuildGetRequest(mUrlName url,wxLongLong start)34 wxString mHTTP::BuildGetRequest(mUrlName url,wxLongLong start)
35 {
36 m_getcommand = wxEmptyString;
37 if (m_use_proxy)
38 m_getcommand << wxT("GET ") << url.GetFullPath() << wxT(" HTTP/1.1\r\n");
39 else
40 m_getcommand << wxT("GET ") << url.GetDir() << url.GetFullRealName() << wxT(" HTTP/1.1\r\n");
41
42 m_headersmsg = wxEmptyString;
43
44
45 // Send authentication information
46 #if wxCHECK_VERSION(2, 8, 0)
47 if (!m_username.IsEmpty() || !m_password.IsEmpty())
48 {
49 m_headersmsg << wxT("Authorization: ") << GenerateAuthString(m_username, m_password) << wxT("\r\n");
50 }
51 #endif
52
53 if (!m_proxy_authstring.IsEmpty())
54 m_headersmsg << wxT("Proxy-Authorization: ") << m_proxy_authstring << wxT("\r\n");
55
56 m_headersmsg << wxT("HOST: ") << url.GetHost() << wxT("\r\n");
57 m_headersmsg << wxT("ACCEPT: */*\r\n");
58 m_headersmsg << wxT("User-Agent: wxDownload Fast\r\n");
59 m_headersmsg << wxT("Range: bytes=") << start << wxT("-\r\n");
60 m_headersmsg << wxT("Pragma: no-cache\r\n");
61 m_headersmsg << wxT("Connection: close\r\n\r\n");
62
63 return m_getcommand + m_headersmsg;
64 }
65
SendGetRequest()66 void mHTTP::SendGetRequest()
67 {
68 if (m_getcommand.IsEmpty())
69 return;
70 char *dados;
71 dados = wxstr2str(m_getcommand);
72 wxHTTP::Write(dados, strlen(dados));
73 delete dados;
74
75 dados = wxstr2str(m_headersmsg);
76 wxHTTP::Write(dados, strlen(dados));
77 delete dados;
78 }
79
ParseHeaders()80 bool mHTTP::ParseHeaders()
81 {
82 wxString line;
83 char buf[8192];
84 bool firstline = TRUE;
85 unsigned int lastcount = 0,count = 0;
86 unsigned int i;
87 wxHTTP::Read(buf,8192);
88 lastcount = wxHTTP::LastCount();
89 do
90 {
91 line.Clear();
92 while ((buf[count] != '\n') && (count <8192))
93 {
94 if ((buf[count] != '\n') && (buf[count] != '\r'))
95 line.Append(buf[count],1);
96 count++;
97 }
98 if (count < 8192)
99 {
100 count++;
101 if (buf[count] == '\r')
102 count++;
103 }
104 else
105 {
106 m_messagereceived += wxT("\nImcomplete message\n");
107 return false;
108 }
109 m_messagereceived += line + wxT("\n");
110 if (!firstline)
111 {
112 wxString left_str = line.BeforeFirst(':');
113 m_headers[left_str] = line.AfterFirst(':').Strip(wxString::both);
114 }
115 else
116 {
117 m_http_response = wxAtoi(line.Mid(9,1));
118 m_http_complete_response = wxAtoi(line.Mid(9,3));
119 firstline = FALSE;
120 }
121 }while (line != wxEmptyString);
122
123 {//PUT IN THE QUEUE THE DATA THAT ISN'T PART OF THE MESSAGE
124 char unreadbuf[8192];
125 for (i = 0 ; i < (lastcount-count);i++)
126 unreadbuf[i] = buf[i+count];
127 wxHTTP::Unread(unreadbuf,i);
128 }
129 return true;
130 }
131
GetResponseMessage()132 wxString mHTTP::GetResponseMessage()
133 {
134 if (m_messagereceived.IsEmpty())
135 {
136 ParseHeaders();
137 }
138 return m_messagereceived;
139 };
140
wxstr2str(wxString wxstr)141 char *mHTTP::wxstr2str(wxString wxstr)
142 {
143 int tamanho = wxstr.Length() + 1;
144 char *data = new char[tamanho];
145 int i;
146 for (i = 0 ; i < (tamanho-1); i++)
147 data[i] = wxstr.GetChar(i);
148 data[i] = '\0';
149 return data;
150 }
151