summaryrefslogtreecommitdiffstats
path: root/tinyXCAP/src/txcap_action.c
blob: df12e9c4613ca3b7b50ed2d31cbd20d880019c0c (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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO 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 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO 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 DOUBANGO.
*
*/
/**@file txcap_action.c
 * @brief XCAP actions.
 *
 * @author Mamadou Diop <diopmamadou(at)doubango.org>
 *

 */
#include "tinyxcap/txcap_action.h"

#include "tsk_options.h"
#include "tsk_params.h"
#include "tinyxcap/txcap_selector.h"

/**@defgroup txcap_action_group XDMC (Sending Requests)
*/

extern char* __txcap_selector_get_url(const txcap_stack_handle_t* stack, const char* auid_id, va_list* app);


/** Sends HTTP/HTTPS request to the XDMS.
*/
int txcap_action_perform(txcap_stack_handle_t* stack, txcap_action_type_t type, txcap_action_target_t target, ...)
{
    tsk_options_L_t* options = tsk_null;
    tsk_params_L_t* headers = tsk_null;
    char* urlstring = tsk_null;
    thttp_action_t* action;
    thttp_dialog_t* dialog;
    int ret = -1;
    txcap_action_param_type_t curr;
    const char* method = "GET";
    const char* mime_type = tsk_null;
    const char* AUID_STR = tsk_null;
    const void* PAY_PTR = tsk_null;
    size_t PAY_SIZE = 0;
    va_list ap;
    txcap_stack_t* xcap_stack = stack;

    if(!xcap_stack) {
        goto bail;
    }
    else {
        xcap_stack = tsk_object_ref(stack);
        options = tsk_list_create();
        headers = tsk_list_create();
    }

    va_start(ap, target);
    while((curr = va_arg(ap, txcap_action_param_type_t)) != txcap_apt_null) {
        switch(curr) {
        case txcap_apt_option: {
            /*(thttp_action_option_t)ID_INT, (const char*)VALUE_STR*/
            thttp_action_option_t ID_INT = va_arg(ap, thttp_action_option_t);
            const char* VALUE_STR = va_arg(ap, const char *);
            tsk_options_add_option(&options, ID_INT, VALUE_STR);
            break;
        }
        case txcap_apt_header: {
            /* (const char*)NAME_STR, (const char*)VALUE_STR */
            const char* NAME_STR = va_arg(ap, const char *);
            const char* VALUE_STR = va_arg(ap, const char *);
            if(VALUE_STR == (const char*)-1) {
                tsk_params_remove_param(headers, NAME_STR);
            }
            else {
                tsk_params_add_param(&headers, NAME_STR, VALUE_STR);
            }
            break;
        }
        case txcap_apt_payload: {
            /*(const void*)PAY_PTR, (size_t)PAY_SIZE*/
            PAY_PTR = va_arg(ap, const void *);
            PAY_SIZE = va_arg(ap, size_t);
            break;
        }
        case txcap_apt_selector: {
            /*(const char*)AUID_STR, __VA_ARGS__*/
            AUID_STR = va_arg(ap, const char *);
            if(!urlstring) {
                urlstring = __txcap_selector_get_url(stack, AUID_STR, &ap);
            }
            break;
        }
        case txcap_apt_urlstring: {
            /* (const char*)URI_STR */
            const char* URI_STR = va_arg(ap, const char *);
            if(!urlstring) {
                urlstring = tsk_strdup(URI_STR);
            }
            break;
        }
        default: {
            TSK_DEBUG_ERROR("NOT SUPPORTED.");
            goto done;
        }
        }/* switch */
    } /* while */

done:
    va_end(ap);

    /* checks urlstring */
    if(!urlstring) {
        TSK_DEBUG_ERROR("Failed to compute XCAP URL");
        goto bail;
    }

    /* HTTP method */
    switch(type) {
    case txcap_atp_create:
    case txcap_atp_replace:
        method = "PUT";
        break;
    case txcap_atp_fetch:
        method = "GET";
        break;
    case txcap_atp_delete:
        method = "DELETE";
        break;
    }

    /*content-type*/
    switch(target) {
    case txcap_atg_element:
        mime_type = TXCAP_MIME_TYPE_ELEMENT;
        break;
    case txcap_atg_document: {
        if(AUID_STR) {
            txcap_auid_t* auid;
            if((auid = txcap_auid_get_by_id(xcap_stack->auids, AUID_STR))) {
                mime_type = (const char*)auid->mime_type; // (cont char*) do not strdup()
                TSK_OBJECT_SAFE_FREE(auid);
            }
            else {
                TSK_DEBUG_WARN("Failed to find auid with id=%s", AUID_STR);
            }
        }
        break;
    }
    case txcap_atg_attribute:
        mime_type = TXCAP_MIME_TYPE_ATTRIBUTE;
        break;
    }

    /* HTTP action */
    if((action = thttp_action_create(thttp_atype_o_request, urlstring, method, tsk_null))) {
        const tsk_list_item_t* item;
        if((dialog = thttp_dialog_new(xcap_stack->http_session))) {
            /* copy options */
            tsk_list_foreach(item, options) {
                tsk_options_add_option(&action->options, ((const tsk_option_t*)item->data)->id, ((const tsk_option_t*)item->data)->value);
            }
            /* copy action-level headers */
            tsk_list_foreach(item, headers) {
                tsk_params_add_param(&action->headers, ((const tsk_param_t*)item->data)->name, ((const tsk_param_t*)item->data)->value);
            }
            /* copy session-level headers ==> will be added by thttp_dialog_send_request()*/
            //--tsk_list_pushback_list(action->headers, ((thttp_session_t*)xcap_stack->http_session)->headers);

            /* Content-Type */
            if(mime_type) {
                if(!tsk_params_have_param(action->headers, "Content-Type")) {
                    tsk_params_add_param(&action->headers, "Content-Type", mime_type);
                }
            }

            /* payload */
            if(PAY_PTR && PAY_SIZE) {
                action->payload = tsk_buffer_create(PAY_PTR, PAY_SIZE);
            }

            /* performs */
            ret = thttp_dialog_fsm_act(dialog, action->type, tsk_null, action);
            tsk_object_unref(dialog);
        }
        else {
            TSK_DEBUG_ERROR("Failed to create new HTTP/HTTPS dialog.");
            ret = -2;
        }
        TSK_OBJECT_SAFE_FREE(action);
    }

bail:
    TSK_FREE(urlstring);
    TSK_OBJECT_SAFE_FREE(options);
    TSK_OBJECT_SAFE_FREE(headers);
    tsk_object_unref(xcap_stack);
    return ret;
}

OpenPOWER on IntegriCloud