aboutsummaryrefslogtreecommitdiff
path: root/tests/http.h
blob: 7eff70136019103e4fdabca32763515ce56ac7ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
//  Http.h
//  Player Javascript
//
//  Created by Anthony Liot on 23/11/12.
//

#ifndef __HTTP_H__
#define __HTTP_H__

#include <string>


/*
 */
class http {
		
	public:
	
		enum Status {
			ST_PENDING = 0,
			ST_FAILED,
			ST_OK
		};

		enum RequestType {
			REQUEST_GET = 0,
			REQUEST_POST ,
		};

		enum AssyncMode {
			ASSYNC_THREAD
		};
		
		// enregistrement sur unigine
    	static void RegisterAsExtension(bool regis);
    	
    	// Callback
		static void onLoaded(void* parent, const char * file);
		static void onError(void* parent, int statuserror);
		static void onProgress(void* parent, int progress);
        
        // Constructeur    
		http(const char* hostname, int requestType, const char* targetFileName = "");
		
		//Destructeur
		virtual ~http();
		
		/**
		* Effectue la requete
		*/
		void runRequest(const char* page, int assync);

		/**
		* Accede a la reponse
		*/
		const char* getContent();

		/**
		* Accede a l'erreur
		*/
		const char* getError(); 

		/**
		* Accede au status
		*/
		int getStatus();

		/**
		* Accede a la progression
		*/
		float getProgress(); 
		
		/**
		* Get Id of http Class
		*/
		int getId(); 

		/**
		*
		*/
		void addValue(const char* key, const char* value);
		
		/**
		* Callback
		*/
		void onProgress(int progress);
		void onLoaded(const char* file);
		void onError(int error);		
				
		// Static parameter	
		static int uid;
		static std::string cross_domain ;
			
	private:
		
		// Id of request
		int 		_uid;
	
		// nom de l'hote
		std::string _hostname;

		// nom de la page
		std::string _page;
		
		// target filename
		std::string _targetFileName;
		
		// param
		std::string _param;

		// resultat
		std::string _content;

		// probleme
		std::string _error;
		
		// request type
		RequestType	_request; 

		// status
		int         _status;

		// progress value
		int         _progressValue;

		// mode assyncrone courant
		AssyncMode  _assync;

};

//this is safe and convenient but not exactly efficient
inline std::string format(const char* fmt, ...){
    int size = 512;
    char* buffer = 0;
    buffer = new char[size];
    va_list vl;
    va_start(vl,fmt);
    int nsize = vsnprintf(buffer,size,fmt,vl);
    if(size<=nsize){//fail delete buffer and try again
        delete buffer; buffer = 0;
        buffer = new char[nsize+1];//+1 for /0
        nsize = vsnprintf(buffer,size,fmt,vl);
    }
    std::string ret(buffer);
    va_end(vl);
    delete buffer;
    return ret;
}

#endif /* __HTTP_H__ */