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;
35 void __transaction_header_cb(http_transaction_h transaction, char *header, size_t header_len, void *user_data)
37 PRG("transaction_header_cb", transaction);
40 void __transaction_body_cb(http_transaction_h transaction, char *body, size_t size, size_t nmemb, void *user_data)
42 PRG("transaction_body_cb", transaction);
43 int written = size * nmemb;
44 DBG("Received: %d\n", written);
47 void __transaction_write_cb(http_transaction_h transaction, int recommended_chunk_size, void *user_data)
49 PRG("transaction_write_cb", transaction);
50 DBG("recommended_chunk_size:%d\n", recommended_chunk_size);
53 void __transaction_completed_cb(http_transaction_h transaction, void *user_data)
55 PRG("transaction_completed_cb", transaction);
57 http_status_code_e status = 0;
61 ret = http_transaction_request_get_uri(transaction, &uri);
62 ret = http_transaction_response_get_status_code(transaction, &status);
64 http_transaction_header_remove_field(transaction, "Content-Length");
65 DBG("%s - status(%d)\n", uri, status);
66 ret = http_transaction_destroy(transaction);
67 if (ret == HTTP_ERROR_NONE) DBG("Success to close transaction\n");
68 else DBG("Fail to close transaction\n");
71 void __transaction_aborted_cb(http_transaction_h transaction, int reason, void *user_data)
73 PRG("transaction_aborted_cb", transaction);
74 DBG("aborted reason: %d\n", reason);
77 void _register_callbacks(http_transaction_h transaction)
79 http_transaction_set_received_header_cb(transaction, __transaction_header_cb, NULL);
80 http_transaction_set_received_body_cb(transaction, __transaction_body_cb, NULL);
81 http_transaction_set_uploaded_cb(transaction, __transaction_write_cb, NULL);
82 http_transaction_set_completed_cb(transaction, __transaction_completed_cb, NULL);
83 http_transaction_set_aborted_cb(transaction, __transaction_aborted_cb, NULL);
86 int test_http_init(void)
88 int ret = http_init();
89 if (ret == HTTP_ERROR_NONE)
94 int test_http_deinit(void)
96 int ret = http_deinit();
97 if (ret == HTTP_ERROR_NONE)
102 int test_http_session_create(void)
107 printf("Select session mode(0: NORMAL 1: PIPELINING): ");
108 ret = scanf("%d", &session_mode);
110 ret = http_session_create(session_mode, &session);
111 if (ret != HTTP_ERROR_NONE) {
112 ERR("Fail to create session", ret);
118 int test_http_session_destroy(void)
120 int ret = http_session_destroy(session);
121 if (ret != HTTP_ERROR_NONE) {
122 ERR("Fail to destroy session", ret);
129 int test_simple_get(void)
133 http_transaction_h transaction = NULL;
134 http_method_e method;
136 printf("Input uri: ");
137 ret = scanf("%1023s", uri);
139 ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction);
141 ERR("Fail to open transaction", ret);
145 http_transaction_request_get_method(transaction, &method);
146 ret = http_transaction_request_set_uri(transaction, uri);
148 ERR("Fail to set URI", ret);
152 _register_callbacks(transaction);
153 ret = http_transaction_submit(transaction);
156 ERR("Fail to submit transaction", ret);
163 int test_multiple_get(void)
166 http_transaction_h transaction[10];
170 printf("Input count of transactions(1~10): ");
171 ret = scanf("%d", &count);
173 for (i = 0; i < count; i++) {
175 printf("Input uri for transaction[%d]: ", i + 1);
176 ret = scanf("%1023s", uri);
177 ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction[i]);
179 ERR("Fail to open transaction", ret);
182 ret = http_transaction_request_set_uri(transaction[i], uri);
184 ERR("Fail to set URI", ret);
187 _register_callbacks(transaction[i]);
188 http_transaction_submit(transaction[i]);
194 int test_simple_post(void)
197 http_transaction_h transaction;
198 const char* post_msg = "name=tizen&project=capi-network-curl";
199 char field_value[15];
201 ret = http_session_open_transaction(session, HTTP_METHOD_POST, &transaction);
203 ERR("Fail to open transaction", ret);
206 ret = http_transaction_request_set_uri(transaction, "http://posttestserver.com/post.php");
208 ERR("Fail to set URI", ret);
211 http_transaction_set_ready_to_write(transaction, TRUE);
212 http_transaction_request_write_body(transaction, post_msg);
214 sprintf(field_value, "%d", strlen(post_msg));
215 printf("[dbg] post size (%s)\n", field_value);
216 http_transaction_header_add_field(transaction, "Content-Length", field_value);
218 _register_callbacks(transaction);
219 http_transaction_submit(transaction);
224 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
229 printf("Event received from stdin\n");
233 if (rv <= 0 || a[0] == '0')
236 if (a[0] == '\n' || a[0] == '\r') {
237 printf("\n\n Network Connection API Test App\n\n");
238 printf("Options..\n");
239 printf("1 - Initialize\n");
240 printf("2 - Deinitialize\n");
241 printf("3 - Create session\n");
242 printf("4 - Destroy session\n");
243 printf("5 - Simple GET\n");
244 printf("6 - Multiple GET\n");
245 printf("7 - Simple POST\n");
247 printf("0 - Exit \n");
248 printf("ENTER - Show options menu.......\n");
253 rv = test_http_init();
256 rv = test_http_deinit();
259 rv = test_http_session_create();
262 rv = test_http_session_destroy();
265 rv = test_simple_get();
268 rv = test_multiple_get();
271 rv = test_simple_post();
276 printf("Operation succeeded!\n");
278 printf("Operation failed!\n");
283 int main(int argc, char **argv)
287 #if !GLIB_CHECK_VERSION(2, 36, 0)
290 mainloop = g_main_loop_new(NULL, false);
292 GIOChannel *channel = g_io_channel_unix_new(0);
293 g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
294 printf("Test Thread created...\n");
295 g_main_loop_run(mainloop);