Changeset 3810 for pjproject/branches/1.x
- Timestamp:
- Oct 11, 2011 4:37:37 AM (13 years ago)
- Location:
- pjproject/branches/1.x/pjlib-util
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/branches/1.x/pjlib-util/include/pjlib-util/http_client.h
r3553 r3810 189 189 pj_http_auth_cred auth_cred; 190 190 191 /** 192 * Optional source port range to use when binding the socket. 193 * This can be used if the source port needs to be within a certain range 194 * for instance due to strict firewall settings. The port used will be 195 * randomized within the range. 196 * 197 * Note that if authentication is configured, the authentication response 198 * will be a new transaction 199 * 200 * Default is 0 (The OS will select the source port automatically) 201 */ 202 pj_uint16_t source_port_range_start; 203 204 /** 205 * Optional source port range to use when binding. 206 * The size of the port restriction range 207 * 208 * Default is 0 (The OS will select the source port automatically)) 209 */ 210 pj_uint16_t source_port_range_size; 211 212 /** 213 * Max number of retries if binding to a port fails. 214 * Note that this does not adress the scenario where a request times out 215 * or errors. This needs to be taken care of by the on_complete callback. 216 * 217 * Default is 3 218 */ 219 pj_uint16_t max_retries; 220 191 221 } pj_http_req_param; 192 222 -
pjproject/branches/1.x/pjlib-util/src/pjlib-util/http_client.c
r3600 r3810 27 27 #include <pj/string.h> 28 28 #include <pj/timer.h> 29 #include <pj/rand.h> 29 30 #include <pjlib-util/base64.h> 30 31 #include <pjlib-util/errno.h> … … 33 34 #include <pjlib-util/string.h> 34 35 36 #define THIS_FILE "http_client.c" 37 35 38 #if 0 36 39 /* Enable some tracing */ 37 #define THIS_FILE "http_client.c"38 40 #define TRACE_(arg) PJ_LOG(3,arg) 39 41 #else … … 765 767 param->timeout.msec = PJ_HTTP_DEFAULT_TIMEOUT; 766 768 pj_time_val_normalize(¶m->timeout); 769 param->max_retries = 3; 767 770 } 768 771 … … 1005 1008 } 1006 1009 1007 PJ_DEF(pj_status_t) pj_http_req_start(pj_http_req *http_req) 1010 static pj_status_t start_http_req(pj_http_req *http_req, 1011 pj_bool_t notify_on_fail) 1008 1012 { 1009 1013 pj_sock_t sock = PJ_INVALID_SOCKET; 1010 1014 pj_status_t status; 1011 1015 pj_activesock_cb asock_cb; 1016 int retry = 0; 1012 1017 1013 1018 PJ_ASSERT_RETURN(http_req, PJ_EINVAL); … … 1032 1037 http_req->addr.ipv4.sin_addr.s_addr==PJ_INADDR_NONE)) 1033 1038 { 1034 return status; // cannot resolve host name1039 goto on_return; 1035 1040 } 1036 1041 http_req->resolved = PJ_TRUE; … … 1046 1051 asock_cb.on_data_sent = &http_on_data_sent; 1047 1052 asock_cb.on_connect_complete = &http_on_connect; 1053 1054 do 1055 { 1056 pj_sockaddr_in bound_addr; 1057 pj_uint16_t port = 0; 1058 1059 /* If we are using port restriction. 1060 * Get a random port within the range 1061 */ 1062 if (http_req->param.source_port_range_start != 0) { 1063 port = (pj_uint16_t) 1064 (http_req->param.source_port_range_start + 1065 (pj_rand() % http_req->param.source_port_range_size)); 1066 } 1067 1068 pj_sockaddr_in_init(&bound_addr, NULL, port); 1069 status = pj_sock_bind(sock, &bound_addr, sizeof(bound_addr)); 1070 1071 } while (status != PJ_SUCCESS && (retry++ < http_req->param.max_retries)); 1072 1073 if (status != PJ_SUCCESS) { 1074 PJ_PERROR(1,(THIS_FILE, status, 1075 "Unable to bind to the requested port")); 1076 goto on_return; 1077 } 1048 1078 1049 1079 // TODO: should we set whole data to 0 by default? … … 1075 1105 if (status == PJ_SUCCESS) { 1076 1106 http_req->state = SENDING_REQUEST; 1077 return http_req_start_sending(http_req); 1107 status = http_req_start_sending(http_req); 1108 if (status != PJ_SUCCESS) 1109 goto on_return; 1078 1110 } else if (status != PJ_EPENDING) { 1079 1111 goto on_return; // error connecting … … 1083 1115 1084 1116 on_return: 1085 http_req_end_request(http_req); 1117 http_req->error = status; 1118 if (notify_on_fail) 1119 pj_http_req_cancel(http_req, PJ_TRUE); 1120 else 1121 http_req_end_request(http_req); 1122 1086 1123 return status; 1124 } 1125 1126 /* Starts an asynchronous HTTP request to the URL specified. */ 1127 PJ_DEF(pj_status_t) pj_http_req_start(pj_http_req *http_req) 1128 { 1129 return start_http_req(http_req, PJ_FALSE); 1087 1130 } 1088 1131 … … 1434 1477 http_req_end_request(hreq); 1435 1478 1436 status = pj_http_req_start(hreq);1479 status = start_http_req(hreq, PJ_TRUE); 1437 1480 if (status != PJ_SUCCESS) 1438 1481 goto on_error;
Note: See TracChangeset
for help on using the changeset viewer.