2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
25 #define ERR(x, y) printf("[ERR] %s(%d)\n", x, y)
26 #define PRG(x, y) printf("[PRG] %s(%p)\n", x, y)
28 #define MAX_URI_LEN 1024
33 http_session_h session = NULL;
34 void _register_callbacks(http_transaction_h transaction);
36 void __transaction_header_cb(http_transaction_h transaction, char *header, size_t header_len, void *user_data)
38 PRG("transaction_header_cb", transaction);
41 void __transaction_body_cb(http_transaction_h transaction, char *body, size_t size, size_t nmemb, void *user_data)
43 PRG("transaction_body_cb", transaction);
44 int written = size * nmemb;
45 DBG("Received: %d\n", written);
48 void __transaction_write_cb(http_transaction_h transaction, int recommended_chunk_size, void *user_data)
50 PRG("transaction_write_cb", transaction);
51 DBG("recommended_chunk_size:%d\n", recommended_chunk_size);
54 void __transaction_completed_cb(http_transaction_h transaction, void *user_data)
56 PRG("transaction_completed_cb", transaction);
58 http_status_code_e status = 0;
64 ret = http_transaction_request_get_uri(transaction, &uri);
65 ret = http_transaction_response_get_status_code(transaction, &status);
66 DBG("%s - status(%d)\n", uri, status);
67 if (status == HTTP_STATUS_UNAUTHORIZED) {
68 DBG("Authentication Required\n");
69 http_transaction_h http_auth_transaction;
70 http_auth_scheme_e auth_scheme = HTTP_AUTH_NONE;
72 http_transaction_open_authentication(transaction, &http_auth_transaction);
73 http_transaction_get_http_auth_scheme(http_auth_transaction, &auth_scheme);
76 ret = scanf("%15s", id);
78 ret = scanf("%15s", pw);
80 http_transaction_set_credentials(http_auth_transaction, id, pw);
81 _register_callbacks(http_auth_transaction);
82 http_transaction_submit(http_auth_transaction);
85 http_transaction_header_remove_field(transaction, "Content-Length");
86 ret = http_transaction_destroy(transaction);
87 if (ret == HTTP_ERROR_NONE) DBG("Success to close transaction\n");
88 else DBG("Fail to close transaction\n");
93 void __transaction_aborted_cb(http_transaction_h transaction, int reason, void *user_data)
95 PRG("transaction_aborted_cb", transaction);
96 DBG("aborted reason: %d\n", reason);
99 void _register_callbacks(http_transaction_h transaction)
101 http_transaction_set_received_header_cb(transaction, __transaction_header_cb, NULL);
102 http_transaction_set_received_body_cb(transaction, __transaction_body_cb, NULL);
103 http_transaction_set_uploaded_cb(transaction, __transaction_write_cb, NULL);
104 http_transaction_set_completed_cb(transaction, __transaction_completed_cb, NULL);
105 http_transaction_set_aborted_cb(transaction, __transaction_aborted_cb, NULL);
108 int test_http_init(void)
110 int ret = http_init();
111 if (ret == HTTP_ERROR_NONE)
116 int test_http_deinit(void)
118 int ret = http_deinit();
119 if (ret == HTTP_ERROR_NONE)
124 int test_http_session_create(void)
129 printf("Select session mode(0: NORMAL 1: PIPELINING): ");
130 ret = scanf("%d", &session_mode);
132 ret = http_session_create(session_mode, &session);
133 if (ret != HTTP_ERROR_NONE) {
134 ERR("Fail to create session", ret);
140 int test_http_session_destroy(void)
142 int ret = http_session_destroy(session);
143 if (ret != HTTP_ERROR_NONE) {
144 ERR("Fail to destroy session", ret);
153 int test_simple_get(void)
157 http_transaction_h transaction = NULL;
158 http_method_e method;
160 printf("Input uri: ");
161 ret = scanf("%1023s", uri);
163 ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction);
165 ERR("Fail to open transaction", ret);
169 http_transaction_request_get_method(transaction, &method);
170 ret = http_transaction_request_set_uri(transaction, uri);
172 ERR("Fail to set URI", ret);
176 _register_callbacks(transaction);
177 ret = http_transaction_submit(transaction);
180 ERR("Fail to submit transaction", ret);
187 int test_multiple_get(void)
190 http_transaction_h transaction[10];
194 printf("Input count of transactions(1~10): ");
195 ret = scanf("%d", &count);
197 for (i = 0; i < count; i++) {
199 printf("Input uri for transaction[%d]: ", i + 1);
200 ret = scanf("%1023s", uri);
201 ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction[i]);
203 ERR("Fail to open transaction", ret);
206 ret = http_transaction_request_set_uri(transaction[i], uri);
208 ERR("Fail to set URI", ret);
211 _register_callbacks(transaction[i]);
212 http_transaction_submit(transaction[i]);
218 int test_simple_post(void)
221 http_transaction_h transaction;
222 const char* post_msg = "name=tizen&project=capi-network-curl";
223 char field_value[15];
225 ret = http_session_open_transaction(session, HTTP_METHOD_POST, &transaction);
227 ERR("Fail to open transaction", ret);
230 ret = http_transaction_request_set_uri(transaction, "http://posttestserver.com/post.php");
232 ERR("Fail to set URI", ret);
235 http_transaction_set_ready_to_write(transaction, TRUE);
236 http_transaction_request_write_body(transaction, post_msg);
238 snprintf(field_value, sizeof(field_value), "%d", (int)strlen(post_msg));
239 printf("[dbg] post size (%s)\n", field_value);
240 http_transaction_header_add_field(transaction, "Content-Length", field_value);
242 _register_callbacks(transaction);
243 http_transaction_submit(transaction);
248 int test_simple_authentication_get(void)
251 http_transaction_h transaction = NULL;
252 http_method_e method;
255 ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction);
257 ERR("Fail to open transaction", ret);
261 printf("Input uri for transaction: ");
262 ret = scanf("%1023s", uri);
264 http_transaction_request_get_method(transaction, &method);
265 ret = http_transaction_request_set_uri(transaction, uri);
267 ERR("Fail to set URI", ret);
271 _register_callbacks(transaction);
272 ret = http_transaction_submit(transaction);
275 ERR("Fail to submit transaction", ret);
282 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
287 printf("Event received from stdin\n");
291 if (rv <= 0 || a[0] == '0')
294 if (a[0] == '\n' || a[0] == '\r') {
295 printf("\n\n Network Connection API Test App\n\n");
296 printf("Options..\n");
297 printf("1 - Initialize\n");
298 printf("2 - Deinitialize\n");
299 printf("3 - Create session\n");
300 printf("4 - Destroy session\n");
301 printf("5 - Simple GET\n");
302 printf("6 - Multiple GET\n");
303 printf("7 - Simple POST\n");
304 printf("8 - Simple Authentication GET\n");
306 printf("0 - Exit \n");
307 printf("ENTER - Show options menu.......\n");
312 rv = test_http_init();
315 rv = test_http_deinit();
318 rv = test_http_session_create();
321 rv = test_http_session_destroy();
324 rv = test_simple_get();
327 rv = test_multiple_get();
330 rv = test_simple_post();
333 rv = test_simple_authentication_get();
338 printf("Operation succeeded!\n");
340 printf("Operation failed!\n");
345 int main(int argc, char **argv)
349 #if !GLIB_CHECK_VERSION(2, 36, 0)
352 mainloop = g_main_loop_new(NULL, false);
354 GIOChannel *channel = g_io_channel_unix_new(0);
355 g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
356 printf("Test Thread created...\n");
357 g_main_loop_run(mainloop);