1 /*
2  * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 
8 #include "httpclient.h"
9 #include "http_wrappers.h"
10 #include "http_form_data.h"
11 
httpclient_common(httpclient_t * client,const char * url,int method,httpclient_data_t * client_data)12 static HTTPC_RESULT httpclient_common(httpclient_t *client, const char *url, int method, httpclient_data_t *client_data)
13 {
14     HTTPC_RESULT ret = HTTP_ECONN;
15 
16     /* reset httpclient redirect flag */
17     client_data->is_redirected = 0;
18 
19     ret = httpclient_conn(client, url);
20 
21     if (!ret) {
22         ret = httpclient_send(client, url, method, client_data);
23 
24         if (!ret) {
25             ret = httpclient_recv(client, client_data);
26         }
27     }
28     /* Don't reset form data when got a redirected response */
29     if(client_data->is_redirected == 0) {
30         httpclient_clear_form_data(client_data);
31     }
32 
33     httpclient_clse(client);
34 
35     return ret;
36 }
37 
httpclient_get(httpclient_t * client,const char * url,httpclient_data_t * client_data)38 HTTPC_RESULT httpclient_get(httpclient_t *client, const char *url, httpclient_data_t *client_data)
39 {
40     int ret = httpclient_common(client, url, HTTP_GET, client_data);
41 
42     while((0 == ret) && (1 == client_data->is_redirected)) {
43         ret = httpclient_common(client, client_data->redirect_url, HTTP_GET, client_data);
44     }
45 
46     if(client_data->redirect_url != NULL) {
47         free(client_data->redirect_url);
48         client_data->redirect_url = NULL;
49 	}
50 
51     return ret;
52 }
53 
httpclient_head(httpclient_t * client,const char * url,httpclient_data_t * client_data)54 HTTPC_RESULT httpclient_head(httpclient_t *client, const char *url, httpclient_data_t *client_data)
55 {
56     int ret = httpclient_common(client, url, HTTP_HEAD, client_data);
57 
58     while((0 == ret) && (1 == client_data->is_redirected)) {
59         ret = httpclient_common(client, client_data->redirect_url, HTTP_HEAD, client_data);
60     }
61 
62     if(client_data->redirect_url != NULL) {
63         free(client_data->redirect_url);
64         client_data->redirect_url = NULL;
65 	}
66 
67     return ret;
68 }
69 
httpclient_post(httpclient_t * client,const char * url,httpclient_data_t * client_data)70 HTTPC_RESULT httpclient_post(httpclient_t *client, const char *url, httpclient_data_t *client_data)
71 {
72     int ret = httpclient_common(client, url, HTTP_POST, client_data);
73 
74     while((0 == ret) && (1 == client_data->is_redirected)) {
75         ret = httpclient_common(client, client_data->redirect_url, HTTP_POST, client_data);
76     }
77 
78     if(client_data->redirect_url != NULL) {
79         free(client_data->redirect_url);
80         client_data->redirect_url = NULL;
81     }
82 
83     return ret;
84 }
85 
httpclient_put(httpclient_t * client,const char * url,httpclient_data_t * client_data)86 HTTPC_RESULT httpclient_put(httpclient_t *client, const char *url, httpclient_data_t *client_data)
87 {
88     int ret = httpclient_common(client, url, HTTP_PUT, client_data);
89 
90     while((0 == ret) && (1 == client_data->is_redirected)) {
91         ret = httpclient_common(client, client_data->redirect_url, HTTP_PUT, client_data);
92     }
93 
94     if(client_data->redirect_url != NULL) {
95         free(client_data->redirect_url);
96         client_data->redirect_url = NULL;
97     }
98 
99     return ret;
100 }
101 
httpclient_delete(httpclient_t * client,const char * url,httpclient_data_t * client_data)102 HTTPC_RESULT httpclient_delete(httpclient_t *client, const char *url, httpclient_data_t *client_data)
103 {
104     return httpclient_common(client, url, HTTP_DELETE, client_data);
105 }
106