aboutsummaryrefslogtreecommitdiffstats
path: root/facebook/facebook-http.h
blob: 6e833385d3b02ca7b37f971ea61109790dcbc33f (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright 2014 James Geboski <jgeboski@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** @file **/

#ifndef _FACEBOOK_HTTP_H
#define _FACEBOOK_HTTP_H

#include <glib.h>
#include <http_client.h>


#define FB_HTTP_CLIENT_FREED   (1 << 31)
#define FB_HTTP_RESEND_MAX     3
#define FB_HTTP_RESEND_TIMEOUT 2000


/**
 * Creates a #fb_http_pair in-line.
 *
 * @param k The key.
 * @param v The value.
 *
 * @return The resulting fb_http_pair.
 **/
#define FB_HTTP_PAIR(k, v) ((fb_http_pair_t *) &((fb_http_pair_t) {k, v}))


/** The #GError codes of #fb_http. **/
typedef enum fb_http_error fb_http_error_t;

/** The flags of #fb_http_req. **/
typedef enum fb_http_req_flags fb_http_req_flags_t;

/** The structure for managing #fb_http_req. **/
typedef struct fb_http fb_http_t;

/** The structure for key/value pairs of strings. **/
typedef struct fb_http_pair fb_http_pair_t;

/** The structure for a #fb_http request. **/
typedef struct fb_http_req fb_http_req_t;


/**
 * The type of callback for #fb_http_req operations.
 *
 * @param req  The #fb_http_req.
 * @param data The user defined data or NULL.
 **/
typedef void (*fb_http_func_t) (fb_http_req_t *req, gpointer data);


/**
 * The #GError codes of #fb_http.
 **/
enum fb_http_error
{
    FB_HTTP_ERROR_CLOSED = 1, /** Closed **/
    FB_HTTP_ERROR_INIT,       /** Initializing **/
    FB_HTTP_ERROR_TIMEOUT,    /** Timeout **/
};

/**
 * The flags of #fb_http_req.
 **/
enum fb_http_req_flags
{
    FB_HTTP_REQ_FLAG_GET  = 1 << 0, /** Use the GET method **/
    FB_HTTP_REQ_FLAG_POST = 1 << 1, /** Use the POST method **/
    FB_HTTP_REQ_FLAG_SSL  = 1 << 2  /** Use encryption via SSL **/
};

/**
 * The structure for managing #fb_http_req.
 **/
struct fb_http
{
    gchar      *agent;   /** The agent. **/
    GHashTable *cookies; /** The #GHashTable of cookies. **/
    GHashTable *reqs;    /** The #GHashTable of #fb_http_req. **/
};

/**
 * The structure for key/value pairs of strings.
 **/
struct fb_http_pair
{
    const gchar *key; /** The key. **/
    const gchar *val; /** The value. **/
};

/**
 * he structure for a #fb_http request.
 **/
struct fb_http_req
{
    fb_http_t           *http;    /** The #fb_http. **/
    fb_http_req_flags_t  flags;   /** The #fb_http_req_flags. **/

    gchar *host;                  /** The hostname. **/
    gint   port;                  /** The port number. **/
    gchar *path;                  /** The pathname. **/
    gint   timeout;               /** The timeout. **/

    GHashTable *headers;          /** The #GHashTable of headers. **/
    GHashTable *params;           /** The #GHashTable of parameters. **/

    fb_http_func_t func;          /** The user callback function or NULL. **/
    gpointer       data;          /** The user define data or NULL. **/

    struct http_request *request; /** The underlying #http_request. **/

    GError *err;                  /** The #GError or NULL. **/
    gchar  *status;               /** Shortcut to request->status_string. **/
    gint    scode;                /** Shortcut to request->status_code. **/
    gchar  *header;               /** Shortcut to request->reply_headers. **/
    gchar  *body;                 /** Shortcut to request->reply_body. **/
    gint    body_size;            /** Shortcut to request->body_size. **/

    gint   toid;                  /** The event ID for the timeout. **/
    guint8 rsc;                   /** The resend count. **/
};


#define FB_HTTP_ERROR fb_http_error_quark()

GQuark fb_http_error_quark(void);

fb_http_t *fb_http_new(const gchar *agent);

void fb_http_free_reqs(fb_http_t *http);

void fb_http_free(fb_http_t *http);

void fb_http_cookies_set(fb_http_t *http, const fb_http_pair_t *pair, ...)
    G_GNUC_NULL_TERMINATED;

void fb_http_cookies_parse_req(fb_http_t *http, const fb_http_req_t *req);

void fb_http_cookies_parse_str(fb_http_t *http, const gchar *data);

gchar *fb_http_cookies_str(fb_http_t *http);

fb_http_req_t *fb_http_req_new(fb_http_t *http, const gchar *host,
                               gint port, const gchar *path,
                               fb_http_func_t func, gpointer data);

void fb_http_req_free(fb_http_req_t *req);

void fb_http_req_headers_set(fb_http_req_t *req, const fb_http_pair_t *pair,
                             ...) G_GNUC_NULL_TERMINATED;

void fb_http_req_params_set(fb_http_req_t *req, const fb_http_pair_t *pair,
                            ...) G_GNUC_NULL_TERMINATED;

void fb_http_req_send(fb_http_req_t *req);

gchar *fb_http_uri_escape(const gchar *unescaped);

gchar *fb_http_uri_unescape(const gchar *escaped);

#endif /* _FACEBOOK_HTTP_H */