Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter_http.c

    rbb5ce4d1 rc2ecadc  
    4141
    4242
    43 static char *twitter_url_append(char *url, char *key, char* value);
     43char *twitter_url_append(char *url, char *key, char* value);
    4444
    4545/**
     
    4747 * This is actually pretty generic function... Perhaps it should move to the lib/http_client.c
    4848 */
    49 void *twitter_http(struct im_connection *ic, char *url_string, http_input_function func, gpointer data, int is_post, char** arguments, int arguments_len)
     49void *twitter_http(char *url_string, http_input_function func, gpointer data, int is_post, char* user, char* pass, struct oauth_info* oi, char** arguments, int arguments_len)
    5050{
    51         struct twitter_data *td = ic->proto_data;
     51        url_t *url = g_new0( url_t, 1 );
    5252        char *tmp;
    53         GString *request = g_string_new("");
     53        char *request;
    5454        void *ret;
     55        char *userpass = NULL;
     56        char *userpass_base64;
    5557        char *url_arguments;
    5658
    57         url_arguments = g_strdup("");
     59        // Fill the url structure.
     60        if( !url_set( url, url_string ) )
     61        {
     62                g_free( url );
     63                return NULL;
     64        }
     65
     66        if( url->proto != PROTO_HTTP && url->proto != PROTO_HTTPS )
     67        {
     68                g_free( url );
     69                return NULL;
     70        }
     71
     72        // Concatenate user and pass
     73        if (user && pass) {
     74                userpass = g_strdup_printf("%s:%s", user, pass);
     75                userpass_base64 = base64_encode((unsigned char*)userpass, strlen(userpass));
     76        } else {
     77                userpass_base64 = NULL;
     78        }
     79
     80        url_arguments = g_malloc(1);
     81        url_arguments[0] = '\0';
    5882
    5983        // Construct the url arguments.
     
    6993        }
    7094
     95        // Do GET stuff...
     96        if (!is_post)
     97        {
     98                // Find the char-pointer of the end of the string.
     99                tmp = url->file + strlen(url->file);
     100                tmp[0] = '?';
     101                // append the url_arguments to the end of the url->file.
     102                // TODO GM: Check the length?
     103                g_stpcpy (tmp+1, url_arguments);
     104        }
     105
     106
    71107        // Make the request.
    72         g_string_printf(request, "%s %s%s%s%s HTTP/1.0\r\n"
    73                                  "Host: %s\r\n"
    74                                  "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n",
    75                                  is_post ? "POST" : "GET",
    76                                  td->url_path, url_string,
    77                                  is_post ? "" : "?", is_post ? "" : url_arguments,
    78                                  td->url_host);
     108        request = g_strdup_printf(  "%s %s HTTP/1.0\r\n"
     109                                    "Host: %s\r\n"
     110                                    "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n",
     111                                    is_post ? "POST" : "GET", url->file, url->host );
    79112
    80113        // If a pass and user are given we append them to the request.
    81         if (td->oauth_info)
     114        if (oi)
    82115        {
    83116                char *full_header;
    84                 char *full_url;
    85117               
    86                 full_url = g_strconcat(set_getstr(&ic->acc->set, "base_url" ), url_string, NULL);
    87                 full_header = oauth_http_header(td->oauth_info, is_post ? "POST" : "GET",
    88                                                 full_url, url_arguments);
     118                full_header = oauth_http_header(oi, is_post ? "POST" : "GET",
     119                                                url_string, url_arguments);
    89120               
    90                 g_string_append_printf(request, "Authorization: %s\r\n", full_header);
     121                tmp = g_strdup_printf("%sAuthorization: %s\r\n", request, full_header);
     122                g_free(request);
    91123                g_free(full_header);
    92                 g_free(full_url);
     124                request = tmp;
    93125        }
    94         else
     126        else if (userpass_base64)
    95127        {
    96                 char userpass[strlen(ic->acc->user)+2+strlen(ic->acc->pass)];
    97                 char *userpass_base64;
    98                
    99                 g_snprintf(userpass, sizeof(userpass), "%s:%s", ic->acc->user, ic->acc->pass);
    100                 userpass_base64 = base64_encode((unsigned char*)userpass, strlen(userpass));
    101                 g_string_append_printf(request, "Authorization: Basic %s\r\n", userpass_base64);
    102                 g_free( userpass_base64 );
     128                tmp = g_strdup_printf("%sAuthorization: Basic %s\r\n", request, userpass_base64);
     129                g_free(request);
     130                request = tmp;
    103131        }
    104132
     
    107135        {
    108136                // Append the Content-Type and url-encoded arguments.
    109                 g_string_append_printf(request,
    110                                        "Content-Type: application/x-www-form-urlencoded\r\n"
    111                                        "Content-Length: %zd\r\n\r\n%s",
    112                                        strlen(url_arguments), url_arguments);
     137                tmp = g_strdup_printf("%sContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %zd\r\n\r\n%s",
     138                                                                request, strlen(url_arguments), url_arguments);
     139                g_free(request);
     140                request = tmp;
    113141        } else {
    114142                // Append an extra \r\n to end the request...
    115                 g_string_append(request, "\r\n");
     143                tmp = g_strdup_printf("%s\r\n", request);
     144                g_free(request);
     145                request = tmp;
    116146        }
    117147
    118         ret = http_dorequest(td->url_host, td->url_port, td->url_ssl, request->str, func, data);
     148        ret = http_dorequest( url->host, url->port,     url->proto == PROTO_HTTPS, request, func, data );
    119149
     150        g_free( url );
     151        g_free( userpass );
     152        g_free( userpass_base64 );
    120153        g_free( url_arguments );
    121         g_string_free( request, TRUE );
     154        g_free( request );
    122155        return ret;
    123156}
    124157
    125 static char *twitter_url_append(char *url, char *key, char* value)
     158char *twitter_url_append(char *url, char *key, char* value)
    126159{
    127160        char *key_encoded = g_strndup(key, 3 * strlen(key));
Note: See TracChangeset for help on using the changeset viewer.