1 /* Copyright (c) MediaArea.net SARL. All Rights Reserved.
2 *
3 * Use of this source code is governed by a zlib-style license that can
4 * be found in the License.txt file in the root of the source tree.
5 */
6
7 //---------------------------------------------------------------------------
8 #include "ZenLib/PreComp.h"
9 #ifdef __BORLANDC__
10 #pragma hdrstop
11 #endif
12 //---------------------------------------------------------------------------
13
14 //---------------------------------------------------------------------------
15 #include "ZenLib/Conf_Internal.h"
16 //---------------------------------------------------------------------------
17
18 //---------------------------------------------------------------------------
19 #include "ZenLib/HTTP_Client.h"
20 #ifdef WINDOWS
21 #undef __TEXT
22 #include <windows.h>
23 #endif //WINDOWS
24 #include "ZenLib/HTTP_Client/HTTPClient.h"
25 using namespace ZenLib;
26 //---------------------------------------------------------------------------
27
28 namespace ZenLib
29 {
30
31 //***************************************************************************
32 // Constructor/Destructor
33 //***************************************************************************
34
35 //---------------------------------------------------------------------------
HTTP_Client()36 HTTP_Client::HTTP_Client ()
37 {
38 #ifdef WINDOWS
39 WSADATA WsaData;
40 WSAStartup(MAKEWORD(1, 2), &WsaData);
41 #endif
42 Handle=0;
43 }
44
45 //---------------------------------------------------------------------------
~HTTP_Client()46 HTTP_Client::~HTTP_Client ()
47 {
48 if (Handle)
49 Close();
50 #ifdef WINDOWS
51 WSACleanup();
52 #endif
53 }
54
55 //***************************************************************************
56 // Open/Close
57 //***************************************************************************
58
59 //---------------------------------------------------------------------------
Open(Ztring Url)60 int HTTP_Client::Open (Ztring Url)
61 {
62 if (Handle)
63 Close();
64
65 //init
66 Handle=HTTPClientOpenRequest(0);
67
68 //Mehtod
69 if (HTTPClientSetVerb(Handle, VerbGet)!=0)
70 {
71 Close();
72 return 0;
73 }
74
75 //Send request
76 if (HTTPClientSendRequest(Handle, (char*)(Url.To_Local().c_str()), NULL, 0, FALSE, 0, 0)!=0)
77 {
78 Close();
79 return 0;
80 }
81
82 //Receive response
83 if (HTTPClientRecvResponse(Handle, 3)!=0)
84 {
85 Close();
86 return 0;
87 }
88
89 return 1;
90 }
91
92 //---------------------------------------------------------------------------
Close()93 void HTTP_Client::Close ()
94 {
95 HTTPClientCloseRequest(&Handle);
96 Handle=0;
97 }
98
99 //***************************************************************************
100 // Read
101 //***************************************************************************
102
103 //---------------------------------------------------------------------------
Read()104 Ztring HTTP_Client::Read ()
105 {
106 if (Handle==0)
107 return Ztring();
108
109 char* Buffer=new char[16384];
110 int32u Size=0;
111
112 int32u ReturnValue=HTTPClientReadData(Handle, Buffer, 16384, 0, &Size);
113 if (ReturnValue!=0 && ReturnValue!=1000) //End of stream
114 {
115 Close();
116 delete[] Buffer;
117 return Ztring();
118 }
119
120 Ztring ToReturn;
121 ToReturn.From_UTF8(Buffer, Size);
122 if (ToReturn.empty())
123 ToReturn.From_Local(Buffer, Size);
124
125 delete[] Buffer;
126 return ToReturn;
127 }
128
129 } //namespace
130