SharingHTTP

SharingHTTP — Class for making simple GET and POST HTTP requests to web services.

Synopsis


#include <sharing-http.h>

gboolean            (*SharingHTTPProgressCallback)      (SharingHTTP *http,
                                                         guint64 bytes_sent,
                                                         gpointer user_data);
enum                SharingHTTPRunResponse;
SharingHTTP*        sharing_http_new                    ();
gint                sharing_http_ref                    (SharingHTTP *self);
gint                sharing_http_unref                  (SharingHTTP *self);
void                sharing_http_set_connection         (SharingHTTP *self,
                                                         ConIcConnection *connection);
ConIcConnection*    sharing_http_get_connection         (SharingHTTP *self);
void                sharing_http_set_timeouts           (SharingHTTP *self,
                                                         glong connecting_timeout,
                                                         glong connection_timeout);
void                sharing_http_set_progress_callback  (SharingHTTP *self,
                                                         SharingHTTPProgressCallback callback,
                                                         gpointer user_data);
SharingHTTPRunResponse sharing_http_run                 (SharingHTTP *self,
                                                         const gchar *url);
void                sharing_http_cancel                 (SharingHTTP *self);
gboolean            sharing_http_add_req_header         (SharingHTTP *self,
                                                         const gchar *name,
                                                         const gchar *value);
gboolean            sharing_http_add_req_header_line    (SharingHTTP *self,
                                                         const gchar *line);
void                sharing_http_remove_req_headers     (SharingHTTP *self);
gboolean            sharing_http_add_req_multipart_file (SharingHTTP *self,
                                                         const gchar *name,
                                                         const gchar *filepath,
                                                         const gchar *type);
gboolean            sharing_http_add_req_multipart_file_with_filename
                                                        (SharingHTTP *self,
                                                         const gchar *name,
                                                         const gchar *filepath,
                                                         const gchar *type,
                                                         const gchar *filename);
gboolean            sharing_http_add_req_multipart_data (SharingHTTP *self,
                                                         const gchar *name,
                                                         const gchar *data,
                                                         gint data_len,
                                                         const gchar *type);
void                sharing_http_clear_multiparts       (SharingHTTP *self);
void                sharing_http_set_res_buffer_size_limit
                                                        (SharingHTTP *self,
                                                         gsize limit);
gsize               sharing_http_get_res_buffer_size_limit
                                                        (SharingHTTP *self);
gint                sharing_http_get_res_code           (SharingHTTP *self);
const gchar*        sharing_http_get_res_content        (SharingHTTP *self,
                                                         gsize *len);
const gchar*        sharing_http_get_res_body           (SharingHTTP *self,
                                                         gsize *len);
void                sharing_http_set_user_agent_name    (SharingHTTP *self,
                                                         const gchar *name);

Description

SharingHTTP makes simple HTTP GET and POST requests to web services. It is designed to be used for uploading media content to services like Flickr and OVI.

SharingHTTP * http = sharing_http_new ();
SharingHTTPRunResponse res;

res = sharing_http_run (http, "http://example.com/post");

if (res == SHARING_HTTP_RUNRES_SUCCESS) {
  g_print ("Got response (%d): %s\n", sharing_http_get_res_code (http),
     sharing_http_get_res_body (http, NULL));
} else {
  g_printerr ("Couldn't get stuff from service\n");
}

sharing_http_unref (http); 

Details

SharingHTTPProgressCallback ()

gboolean            (*SharingHTTPProgressCallback)      (SharingHTTP *http,
                                                         guint64 bytes_sent,
                                                         gpointer user_data);

Progress callback function prototype for SharingHTTP

http : Pointer to SharingHTTP object calling
bytes_sent : How many bytes is sent
user_data : User data pointer
Returns : TRUE to continue, FALSE to cancel transfer

enum SharingHTTPRunResponse

typedef enum {
    SHARING_HTTP_RUNRES_SUCCESS,
    SHARING_HTTP_RUNRES_UNKNOWN_FAILURE, 
    SHARING_HTTP_RUNRES_INVALID_PARAMETERS,
    SHARING_HTTP_RUNRES_CONNECTION_PROBLEM,
    SHARING_HTTP_RUNRES_CANCELLED,
    SHARING_HTTP_RUNRES_ALREADY_RUNNING
} SharingHTTPRunResponse;

Response codes for sharing_http_run()

SHARING_HTTP_RUNRES_SUCCESS Run was a success.
SHARING_HTTP_RUNRES_UNKNOWN_FAILURE Unknown failure.
SHARING_HTTP_RUNRES_INVALID_PARAMETERS Invalid parameters given.
SHARING_HTTP_RUNRES_CONNECTION_PROBLEM Couldn't get connection to URL.
SHARING_HTTP_RUNRES_CANCELLED Run was cancelled by callback.
SHARING_HTTP_RUNRES_ALREADY_RUNNING There is already active process ongoing.

sharing_http_new ()

SharingHTTP*        sharing_http_new                    ();

Create a new SharingHTTP object.

Returns : a newly-allocated SharingHTTP with a reference count of 1. Decrease ther reference count with sharing_http_unref().

sharing_http_ref ()

gint                sharing_http_ref                    (SharingHTTP *self);

Increase the reference count of a SharingHTTP object.

self : Pointer to SharingHTTP object
Returns : the new reference count

sharing_http_unref ()

gint                sharing_http_unref                  (SharingHTTP *self);

Decrease the reference count of a SharingHTTP object.

self : a SharingHTTP object
Returns : the new reference count. If the reference count is less than 1, the SharingHTTP object was freed

sharing_http_set_connection ()

void                sharing_http_set_connection         (SharingHTTP *self,
                                                         ConIcConnection *connection);

Set the network connection used for HTTP requests.

self : a SharingHTTP object
connection : the Internet connection to use

sharing_http_get_connection ()

ConIcConnection*    sharing_http_get_connection         (SharingHTTP *self);

Get the network connection used for HTTP requests.

self : a SharingHTTP object
Returns : the connection that will be used, or NULL if no connection is specified

sharing_http_set_timeouts ()

void                sharing_http_set_timeouts           (SharingHTTP *self,
                                                         glong connecting_timeout,
                                                         glong connection_timeout);

Change the timeout values of a SharingHTTP request.

self : a SharingHTTP object
connecting_timeout : how long we wait connecting in seconds
connection_timeout : how long the connection can last in seconds

sharing_http_set_progress_callback ()

void                sharing_http_set_progress_callback  (SharingHTTP *self,
                                                         SharingHTTPProgressCallback callback,
                                                         gpointer user_data);

Change the callback called while a transfer is in progress.

self : a SharingHTTP object
callback : the callback called while the transfer is in progress, or NULL to unset
user_data : the data pointer passed to the callback function

sharing_http_run ()

SharingHTTPRunResponse sharing_http_run                 (SharingHTTP *self,
                                                         const gchar *url);

Run a SharingHTTP request and get a response.

self : a SharingHTTP object
url : the URL where the request is sent
Returns : the result of the SharingHTTP request

sharing_http_cancel ()

void                sharing_http_cancel                 (SharingHTTP *self);

Cancel the current request of a SharingHTTP object.

self : a SharingHTTP object

sharing_http_add_req_header ()

gboolean            sharing_http_add_req_header         (SharingHTTP *self,
                                                         const gchar *name,
                                                         const gchar *value);

Add a new header to an HTTP request, by combining the name and value into a single header line.

self : a SharingHTTP object
name : name of the header to add
value : value of the header to add
see_also : sharing_http_add_req_header_line()
Returns : TRUE if the header was added successfully, FALSE otherwise

sharing_http_add_req_header_line ()

gboolean            sharing_http_add_req_header_line    (SharingHTTP *self,
                                                         const gchar *line);

Add a new header line to an HTTP request.

self : a SharingHTTP object
line : a header line to add
Returns : TRUE if the header line was added successfully, FALSE otherwise

sharing_http_remove_req_headers ()

void                sharing_http_remove_req_headers     (SharingHTTP *self);

sharing_http_add_req_multipart_file ()

gboolean            sharing_http_add_req_multipart_file (SharingHTTP *self,
                                                         const gchar *name,
                                                         const gchar *filepath,
                                                         const gchar *type);

Add a file to a SharingHTTP request as a multipart, and substitue the on-disk file name for a specified alternative.

self : a SharingHTTP object
name : the name of the part
filepath : the path to the file
type : the type of the part
filename : a filename to use in the request, rather than the the on-disk filename, or NULL to use the on-disk filename
Returns : TRUE if the file was added successfully, FALSE otherwise

sharing_http_add_req_multipart_file_with_filename ()

gboolean            sharing_http_add_req_multipart_file_with_filename
                                                        (SharingHTTP *self,
                                                         const gchar *name,
                                                         const gchar *filepath,
                                                         const gchar *type,
                                                         const gchar *filename);

sharing_http_add_req_multipart_data ()

gboolean            sharing_http_add_req_multipart_data (SharingHTTP *self,
                                                         const gchar *name,
                                                         const gchar *data,
                                                         gint data_len,
                                                         const gchar *type);

Add data as a multipart to a SharingHTTP request.

self : a SharingHTTP object
name : the name of the part
data : the data to add to the request
data_len : the length of the data, or -1 if the data is NULL-terminated
type : the content type of the data
Returns : TRUE if the data was added successfully, FALSE

sharing_http_clear_multiparts ()

void                sharing_http_clear_multiparts       (SharingHTTP *self);

Clear all multipart data from a SharingHTTP request.

self : a SharingHTTP object

sharing_http_set_res_buffer_size_limit ()

void                sharing_http_set_res_buffer_size_limit
                                                        (SharingHTTP *self,
                                                         gsize limit);

Set the size limit of the response buffer of a SharingHTTP request.

self : a SharingHTTP object
limit : the new size limit in bytes

sharing_http_get_res_buffer_size_limit ()

gsize               sharing_http_get_res_buffer_size_limit
                                                        (SharingHTTP *self);

Get the size limit of the response buffer of a SharingHTTP request.

self : a SharingHTTP object
Returns : the size limit of the response buffer in bytes, or 0 if no size limit is specified

sharing_http_get_res_code ()

gint                sharing_http_get_res_code           (SharingHTTP *self);

Get the HTTP response code of a SharingHTTP request.

self : a SharingHTTP object
Returns : an HTTP response code, or -1 if no response code is specified

sharing_http_get_res_content ()

const gchar*        sharing_http_get_res_content        (SharingHTTP *self,
                                                         gsize *len);

Get the full content (both the header and body) of a SharingHTTP response.

self : a SharingHTTP object
len : an output parameter for the length of the response content, which is required if the content is not NULL-terminated
Returns : the response content on success, or NULL otherwise

sharing_http_get_res_body ()

const gchar*        sharing_http_get_res_body           (SharingHTTP *self,
                                                         gsize *len);

Get the body of a SharingHTTP response.

self : a SharingHTTP object
len : an output parameter for the length of the response body, which is required if the response is not NULL-terminated
Returns : the response body on success, or NULL otherwise

sharing_http_set_user_agent_name ()

void                sharing_http_set_user_agent_name    (SharingHTTP *self,
                                                         const gchar *name);

Set the user agent sent with a SharingHTTP request.

self : a SharingHTTP object
name : the user agent name, or NULL if none