1 //
2 // Http.h
3 // Player Javascript
4 //
5 // Created by Anthony Liot on 23/11/12.
6 //
7
8 #ifndef __HTTP_H__
9 #define __HTTP_H__
10
11 #include <stdarg.h>
12 #include <string>
13
14
15 /*
16 */
17 class http {
18
19 public:
20
21 enum Status {
22 ST_PENDING = 0,
23 ST_FAILED,
24 ST_OK,
25 ST_ABORTED,
26 };
27
28 enum RequestType {
29 REQUEST_GET = 0,
30 REQUEST_POST ,
31 };
32
33 enum AssyncMode {
34 ASSYNC_THREAD
35 };
36
37 // enregistrement sur unigine
38 static void RegisterAsExtension(bool regis);
39
40 // Callback
41 static void onLoaded(unsigned handle, void* parent, const char * file);
42 static void onError(unsigned handle, void* parent, int statuserror);
43 static void onProgress(unsigned handle, void* parent, int progress);
44
45 // Constructeur
46 http(const char* hostname, int requestType, const char* targetFileName = "");
47
48 //Destructeur
49 virtual ~http();
50
51 /**
52 * Effectue la requete
53 */
54 void runRequest(const char* page, int assync);
55
56 /**
57 * Abort the request
58 */
59 void abortRequest();
60
61 /**
62 * Accede a la reponse
63 */
64 const char* getContent();
65
66 /**
67 * Accede a l'erreur
68 */
69 const char* getError();
70
71 /**
72 * Accede au status
73 */
74 int getStatus();
75
76 /**
77 * Accede a la progression
78 */
79 float getProgress();
80
81 /**
82 * Get Id of http Class
83 */
84 int getId();
85
86 /**
87 *
88 */
89 void addValue(const char* key, const char* value);
90
91 /**
92 * Callback
93 */
94 void onProgress(int progress);
95 void onLoaded(const char* file);
96 void onError(int error);
97
98 // Static parameter
99 static int uid;
100 static std::string cross_domain ;
101
102 private:
103
104 // Id of request
105 int _uid;
106
107 // nom de l'hote
108 std::string _hostname;
109
110 // nom de la page
111 std::string _page;
112
113 // target filename
114 std::string _targetFileName;
115
116 // param
117 std::string _param;
118
119 // resultat
120 std::string _content;
121
122 // probleme
123 std::string _error;
124
125 // request type
126 RequestType _request;
127
128 // status
129 int _status;
130
131 // progress value
132 int _progressValue;
133
134 // mode assyncrone courant
135 AssyncMode _assync;
136
137 // request handle
138 unsigned _handle;
139
140 };
141
142 //this is safe and convenient but not exactly efficient
format(const char * fmt,...)143 inline std::string format(const char* fmt, ...){
144 int size = 512;
145 char* buffer = 0;
146 buffer = new char[size];
147 va_list vl;
148 va_start(vl,fmt);
149 int nsize = vsnprintf(buffer,size,fmt,vl);
150 if(size<=nsize){//fail delete buffer and try again
151 delete buffer; buffer = 0;
152 buffer = new char[nsize+1];//+1 for /0
153 nsize = vsnprintf(buffer,size,fmt,vl);
154 }
155 std::string ret(buffer);
156 va_end(vl);
157 delete buffer;
158 return ret;
159 }
160
161 #endif /* __HTTP_H__ */
162