openssl 实现https 网页访问

openssl 实现https 网页访问

原文https://blog.csdn.net/xiongtiancheng/article/details/86564596

下面是一个用openssl  实现获取https 网页内容的demo,整个流程比较简单,主要封装的API如下

static int https_init(https_context_t *context,const char* url);
static int https_uninit(https_context_t *context);
static int https_read(https_context_t *context,void* buff,int len);
static int https_write(https_context_t *context,const void* buff,int len);
static int https_get_status_code(https_context_t *context);
static int https_read_content(https_context_t *context,char *resp_contet,int max_len);

通信流程:

1、初始化,解析url资源,创建socket 连接,绑定ssl

2、发送http 请求

3、获取请求返回的状态码

4、获取请求返回的数据

5、销毁动态申请的内存资源

 

为了方便学习,我就把整个代码贴在下面

 
  1.  
  2. #include <stdio.h>

  3. #include <string.h>

  4. #include <stdlib.h>

  5. #include <unistd.h>

  6. #include <sys/stat.h>

  7. #include <sys/socket.h>

  8. #include <netinet/in.h>

  9. #include <arpa/inet.h>

  10. #include <netdb.h>

  11. #include <openssl/ssl.h>

  12. #include <openssl/bio.h>

  13.  
  14. #define HTTP_REQ_LENGTH 512

  15. #define HTTP_RESP_LENGTH 20480

  16.  
  17. typedef struct

  18. {

  19. int sock_fd;

  20. SSL_CTX *ssl_ct;

  21. SSL *ssl;

  22.  
  23. //url 解析出来的信息

  24. char *host;

  25. char *path;

  26. int port;

  27. } https_context_t;

  28.  
  29. static int https_init(https_context_t *context,const char* url);

  30. static int https_uninit(https_context_t *context);

  31. static int https_read(https_context_t *context,void* buff,int len);

  32. static int https_write(https_context_t *context,const void* buff,int len);

  33. static int https_get_status_code(https_context_t *context);

  34. static int https_read_content(https_context_t *context,char *resp_contet,int max_len);

  35.  
  36. // http 请求头信息

  37. static char https_header[] =

  38. "GET %s HTTP/1.1\r\n"

  39. "Host: %s:%d\r\n"

  40. "Connection: Close\r\n"

  41. "Accept: */*\r\n"

  42. "\r\n";

  43.  
  44. static char http_req_content[HTTP_REQ_LENGTH] = {0};

  45. static char https_resp_content[HTTP_RESP_LENGTH+1] = {0};

  46.  
  47. static int create_request_socket(const char* host,const int port)

  48. {

  49. int sockfd;

  50. struct hostent *server;

  51. struct sockaddr_in serv_addr;

  52.  
  53. sockfd = socket(AF_INET, SOCK_STREAM, 0);

  54. if (sockfd < 0)

  55. {

  56. printf("[http_demo] create_request_socket create socket fail.\n");

  57. return -1;

  58. }

  59.  
  60. /* lookup the ip address */

  61. server = gethostbyname(host);

  62. if(server == NULL)

  63. {

  64. printf("[http_demo] create_request_socket gethostbyname fail.\n");

  65. close(sockfd);

  66. return -1;

  67. }

  68.  
  69. memset(&serv_addr,0,sizeof(serv_addr));

  70. serv_addr.sin_family = AF_INET;

  71. serv_addr.sin_port = htons(port);

  72. memcpy(&serv_addr.sin_addr.s_addr,server->h_addr,server->h_length);

  73.  
  74. if (connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)

  75. {

  76. printf("[http_demo] create_request_socket connect fail.\n");

  77. close(sockfd);

  78. return -1;

  79. }

  80. return sockfd;

  81. }

  82.  
  83. /**

  84. * @brief https_parser_url 解析出https 中的域名、端口和路径

  85. * @param url 需要解析的url

  86. * @param host 解析出来的域名或者ip

  87. * @param port 端口,没有时默认返回443

  88. * @param path 路径,指的是域名后面的位置

  89. * @return

  90. */

  91. static int https_parser_url(const char* url,char **host,int *port,char **path)

  92. {

  93. if(url == NULL || strlen(url) < 9 || host == NULL || path == NULL)

  94. {

  95. printf("[https_demo] url or host or path is null.\n");

  96. return -1;

  97. }

  98.  
  99. //判断是不是 https://

  100. int i = 0;

  101. char https_prefix[] = "https://";

  102. for(i=0;i<8;i++)

  103. {

  104. if(url[i] != https_prefix[i])

  105. {

  106. printf("[https_demo] illegal url = %s.\n",url);

  107. return -1;

  108. }

  109. }

  110.  
  111. const char *temp = url+i;

  112. while(*temp != '/') //next /

  113. {

  114. if(*temp == '\0') //

  115. {

  116. printf("[https_demo] illegal url = %s.\n",url);

  117. return -1;

  118. }

  119. temp++;

  120. }

  121.  
  122. const char *host_port = url+i;

  123. while(*host_port != ':' && *host_port != '/') //找到 :或者 / 结束

  124. {

  125. host_port ++;

  126. }

  127.  
  128. int host_len = host_port-url-i; //减掉https://

  129. int path_len = strlen(temp);

  130. char *host_temp = (char *)malloc(host_len + 1); //多一个字符串结束标识 \0

  131. if(host_temp == NULL)

  132. {

  133. printf("[https_demo] malloc host fail.\n");

  134. return -1;

  135. }

  136. if(*host_port++ == ':') //url 中有端口

  137. {

  138. *port = 0;

  139. while(*host_port !='/' && *host_port !='\0') //十进制字符串转成数字

  140. {

  141. *port *= 10;

  142. *port += (*host_port - '0');

  143. host_port ++;

  144. }

  145. }

  146. else

  147. {

  148. *port = 443;

  149. }

  150.  
  151. char *path_temp = (char *)malloc(path_len + 1); //多一个字符串结束标识 \0

  152. if(path_temp == NULL)

  153. {

  154. printf("[https_demo] malloc path fail.\n");

  155. free(host_temp);

  156. return -1;

  157. }

  158. memcpy(host_temp,url+i,host_len);

  159. memcpy(path_temp,temp,path_len);

  160. host_temp[host_len] = '\0';

  161. path_temp[path_len] = '\0';

  162. *host = host_temp;

  163. *path = path_temp;

  164. return 0;

  165. }

  166.  
  167. static int https_init(https_context_t *context,const char* url)

  168. {

  169. if(context == NULL)

  170. {

  171. printf("[https_demo] init https_context_t is null.\n");

  172. return -1;

  173. }

  174.  
  175. if(https_parser_url(url,&(context->host),&(context->port),&(context->path)))

  176. {

  177. printf("[https_demo] https_parser_url fail.\n");

  178. return -1;

  179. }

  180.  
  181. context->sock_fd = create_request_socket(context->host,context->port);

  182. if(context->sock_fd < 0)

  183. {

  184. printf("[https_demo] create_request_socket fail.\n");

  185. goto https_init_fail;

  186. }

  187.  
  188. context->ssl_ct = SSL_CTX_new(SSLv23_method());

  189. if(context->ssl_ct == NULL)

  190. {

  191. printf("[https_demo] SSL_CTX_new fail.\n");

  192. goto https_init_fail;

  193. }

  194.  
  195. context->ssl = SSL_new(context->ssl_ct);

  196. if(context->ssl == NULL)

  197. {

  198. printf("[https_demo] SSL_new fail.\n");

  199. goto https_init_fail;

  200. }

  201.  
  202. if(SSL_set_fd(context->ssl,context->sock_fd)<0)

  203. {

  204. printf("[https_demo] SSL_set_fd fail \n");

  205. }

  206.  
  207. if(SSL_connect(context->ssl) == -1)

  208. {

  209. printf("[https_demo] SSL_connect fail.\n");

  210. goto https_init_fail;

  211. }

  212. return 0;

  213. https_init_fail:

  214. https_uninit(context);

  215. return -1;

  216. }

  217.  
  218. static int https_read(https_context_t *context,void* buff,int len)

  219. {

  220. if(context == NULL || context->ssl == NULL)

  221. {

  222. printf("[https_demo] read https_context_t or ssl is null.\n");

  223. return -1;

  224. }

  225. return SSL_read(context->ssl,buff,len);

  226. }

  227.  
  228. static int https_write(https_context_t *context,const void* buff,int len)

  229. {

  230. if(context == NULL || context->ssl == NULL)

  231. {

  232. printf("[https_demo] write https_context_t or ssl is null.\n");

  233. return -1;

  234. }

  235. return SSL_write(context->ssl,buff,len);

  236. }

  237.  
  238. static int https_get_status_code(https_context_t *context)

  239. {

  240. if(context == NULL || context->ssl == NULL)

  241. {

  242. printf("[https_demo] get status https_context_t or ssl is null.\n");

  243. return -1;

  244. }

  245. int ret;

  246. int flag =0;

  247. int recv_len = 0;

  248. char res_header[1024] = {0};

  249. while(recv_len<1023)

  250. {

  251. ret = SSL_read(context->ssl, res_header+recv_len, 1);

  252. if(ret<1) // recv fail

  253. {

  254. break;

  255. }

  256. //找到响应头的头部信息, 两个"\r\n"为分割点

  257. if((res_header[recv_len]=='\r'&&(flag==0||flag==2))||(res_header[recv_len]=='\n'&&(flag==1||flag==3)))

  258. {

  259. flag++;

  260. }

  261. else

  262. {

  263. flag = 0;

  264. }

  265. recv_len+=ret;

  266. if(flag==4)

  267. {

  268. break;

  269. }

  270. }

  271. //printf("[http_demo] recv_len=%d res_header = %s.\n",recv_len,res_header);

  272. /*获取响应头的信息*/

  273. int status_code = -1;

  274. char *pos = strstr(res_header, "HTTP/");

  275. if(pos)

  276. {

  277. sscanf(pos, "%*s %d", &status_code);//返回状态码

  278. }

  279. return status_code;

  280. }

  281.  
  282. static int https_read_content(https_context_t *context,char *resp_contet,int max_len)

  283. {

  284. if(context == NULL || context->ssl == NULL)

  285. {

  286. printf("[https_demo] read content https_context_t or ssl is null.\n");

  287. return -1;

  288. }

  289. int ret ;

  290. int recv_size = 0;

  291. while(recv_size < max_len)

  292. {

  293. ret = SSL_read(context->ssl,resp_contet + recv_size,max_len-recv_size);

  294. if(ret < 1)

  295. {

  296. break;

  297. }

  298. recv_size += ret;

  299. }

  300. return recv_size;

  301. }

  302.  
  303. static int https_uninit(https_context_t *context)

  304. {

  305. if(context == NULL)

  306. {

  307. printf("[https_demo] uninit https_context_t is null.\n");

  308. return -1;

  309. }

  310.  
  311. if(context->host != NULL)

  312. {

  313. free(context->host);

  314. context->host = NULL;

  315. }

  316. if(context->path != NULL)

  317. {

  318. free(context->path);

  319. context->path = NULL;

  320. }

  321.  
  322. if(context->ssl != NULL)

  323. {

  324. SSL_shutdown(context->ssl);

  325. //SSl_free(context->ssl);

  326. context->ssl = NULL;

  327. }

  328. if(context->ssl_ct != NULL)

  329. {

  330. SSL_CTX_free(context->ssl_ct);

  331. context->ssl_ct = NULL;

  332. }

  333. if(context->sock_fd > 0)

  334. {

  335. close(context->sock_fd);

  336. context->sock_fd = -1;

  337. }

  338. return 0;

  339. }

  340.  
  341. int main()

  342. {

  343. https_context_t https_ct = {0};

  344. int ret = SSL_library_init();

  345. printf("[https_demo] SSL_library_init ret = %d.\n",ret);

  346.  
  347. https_init(&https_ct,"https://www.baidu.com/");

  348.  
  349. ret = snprintf(http_req_content,HTTP_REQ_LENGTH,https_header,https_ct.path,https_ct.host,https_ct.port);

  350.  
  351. ret = https_write(&https_ct,http_req_content,ret);

  352. printf("[https_demo] https_write ret = %d.\n",ret);

  353.  
  354. if(https_get_status_code(&https_ct) == 200)

  355. {

  356. ret = https_read_content(&https_ct,https_resp_content,HTTP_RESP_LENGTH);

  357. if(ret > 0)

  358. {

  359. https_resp_content[ret] = '\0'; //字符串结束标识

  360. printf("[https_demo] https_write https_resp_content = \n %s.\n",https_resp_content);

  361. }

  362. }

  363. https_uninit(&https_ct);

  364. return 0;

  365. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值