-
Notifications
You must be signed in to change notification settings - Fork 9
/
curl.h
76 lines (58 loc) · 2.12 KB
/
curl.h
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
#ifndef CURL_H
#define CURL_H
#include <v8.h>
#include <node.h>
#include "../deps/curl/include/curl/curl.h"
#include <vector>
using namespace v8;
// Global object exposed to the world
class NodeCurl : public node::ObjectWrap {
public:
static void Init (Handle<Object> target);
// Public APIs
static Handle<Value> request (const Arguments&);
static Handle<Value> get (const Arguments&);
static Handle<Value> post (const Arguments&);
};
// Request Object
class Request {
public:
Request ();
~Request ();
static Handle<Value> New (Handle<Object>);
// Public APIs
// request.write(chunk)
static Handle<Value> write (const Arguments&);
// request.end([chunk])
static Handle<Value> end (const Arguments&);
// request.endFile(filename)
static Handle<Value> endFile (const Arguments&);
private:
// Generate new Rquest Object
static Handle<ObjectTemplate> NewTemplate ();
// Translate curl result to Object
Handle<Object> GetResult () const;
// Add custom headers
void AddHeaders (Handle<Object> headers) const;
// Parse headers to object
Handle<Object> ParseHeaders () const;
// Set content-length according to method
void SetContentLength (size_t) const;
// Callbacks with curl
static size_t read_data (void *ptr, size_t size, size_t nmemb, void *userdata);
static size_t write_data (void *ptr, size_t size, size_t nmemb, void *userdata);
CURL *curl_;
typedef std::vector<char> buffer_t;
buffer_t read_buffer_, write_buffer_, header_buffer_;
size_t read_pos_;
enum { METHOD_LEN = 64 };
char method_[METHOD_LEN];
protected:
static inline Request* Unwrap (v8::Handle<v8::Object> handle);
inline void Wrap (v8::Handle<v8::Object> handle);
};
#define THROW_BAD_ARGS \
ThrowException(Exception::TypeError(String::New("Bad arguments")))
#define THROW_TYPE_ERROR(msg) \
ThrowException(Exception::TypeError(String::New(msg)))
#endif /* end of CURL_H */