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");
73 void __transaction_aborted_cb(http_transaction_h transaction, int reason, void *user_data)
75 PRG("transaction_aborted_cb", transaction);
76 DBG("aborted reason: %d\n", reason);
79 void _register_callbacks(http_transaction_h transaction)
81 http_transaction_set_received_header_cb(transaction, __transaction_header_cb, NULL);
82 http_transaction_set_received_body_cb(transaction, __transaction_body_cb, NULL);
83 http_transaction_set_uploaded_cb(transaction, __transaction_write_cb, NULL);
84 http_transaction_set_completed_cb(transaction, __transaction_completed_cb, NULL);
85 http_transaction_set_aborted_cb(transaction, __transaction_aborted_cb, NULL);
88 int test_http_init(void)
90 int ret = http_init();
91 if (ret == HTTP_ERROR_NONE)
96 int test_http_deinit(void)
98 int ret = http_deinit();
99 if (ret == HTTP_ERROR_NONE)
104 int test_http_session_create(void)
109 printf("Select session mode(0: NORMAL 1: PIPELINING): ");
110 ret = scanf("%d", &session_mode);
112 ret = http_session_create(session_mode, &session);
113 if (ret != HTTP_ERROR_NONE) {
114 ERR("Fail to create session", ret);
120 int test_http_session_destroy(void)
122 int ret = http_session_destroy(session);
123 if (ret != HTTP_ERROR_NONE) {
124 ERR("Fail to destroy session", ret);
133 int test_simple_get(void)
137 http_transaction_h transaction = NULL;
138 http_method_e method;
140 printf("Input uri: ");
141 ret = scanf("%1023s", uri);
143 ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction);
145 ERR("Fail to open transaction", ret);
149 http_transaction_request_get_method(transaction, &method);
150 ret = http_transaction_request_set_uri(transaction, uri);
152 ERR("Fail to set URI", ret);
156 _register_callbacks(transaction);
157 ret = http_transaction_submit(transaction);
160 ERR("Fail to submit transaction", ret);
167 int test_multiple_get(void)
170 http_transaction_h transaction[10];
174 printf("Input count of transactions(1~10): ");
175 ret = scanf("%d", &count);
177 for (i = 0; i < count; i++) {
179 printf("Input uri for transaction[%d]: ", i + 1);
180 ret = scanf("%1023s", uri);
181 ret = http_session_open_transaction(session, HTTP_METHOD_GET, &transaction[i]);
183 ERR("Fail to open transaction", ret);
186 ret = http_transaction_request_set_uri(transaction[i], uri);
188 ERR("Fail to set URI", ret);
191 _register_callbacks(transaction[i]);
192 http_transaction_submit(transaction[i]);
198 int test_simple_post(void)
201 http_transaction_h transaction;
202 const char* post_msg = "name=tizen&project=capi-network-curl";
203 char field_value[15];
205 ret = http_session_open_transaction(session, HTTP_METHOD_POST, &transaction);
207 ERR("Fail to open transaction", ret);
210 ret = http_transaction_request_set_uri(transaction, "http://posttestserver.com/post.php");
212 ERR("Fail to set URI", ret);
215 http_transaction_set_ready_to_write(transaction, TRUE);
216 http_transaction_request_write_body(transaction, post_msg);
218 sprintf(field_value, "%d", strlen(post_msg));
219 printf("[dbg] post size (%s)\n", field_value);
220 http_transaction_header_add_field(transaction, "Content-Length", field_value);
222 _register_callbacks(transaction);
223 http_transaction_submit(transaction);
228 gboolean test_thread(GIOChannel *source, GIOCondition condition, gpointer data)
233 printf("Event received from stdin\n");
237 if (rv <= 0 || a[0] == '0')
240 if (a[0] == '\n' || a[0] == '\r') {
241 printf("\n\n Network Connection API Test App\n\n");
242 printf("Options..\n");
243 printf("1 - Initialize\n");
244 printf("2 - Deinitialize\n");
245 printf("3 - Create session\n");
246 printf("4 - Destroy session\n");
247 printf("5 - Simple GET\n");
248 printf("6 - Multiple GET\n");
249 printf("7 - Simple POST\n");
251 printf("0 - Exit \n");
252 printf("ENTER - Show options menu.......\n");
257 rv = test_http_init();
260 rv = test_http_deinit();
263 rv = test_http_session_create();
266 rv = test_http_session_destroy();
269 rv = test_simple_get();
272 rv = test_multiple_get();
275 rv = test_simple_post();
280 printf("Operation succeeded!\n");
282 printf("Operation failed!\n");
287 int main(int argc, char **argv)
291 #if !GLIB_CHECK_VERSION(2, 36, 0)
294 mainloop = g_main_loop_new(NULL, false);
296 GIOChannel *channel = g_io_channel_unix_new(0);
297 g_io_add_watch(channel, (G_IO_IN|G_IO_ERR|G_IO_HUP|G_IO_NVAL), test_thread, NULL);
298 printf("Test Thread created...\n");
299 g_main_loop_run(mainloop);