VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/misc/http.cpp@ 43651

最後變更 在這個檔案從43651是 43651,由 vboxsync 提交於 12 年 前

Runtime/http: typo

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 6.1 KB
 
1
2/* $Id: http.cpp 43651 2012-10-16 09:25:28Z vboxsync $ */
3/** @file
4 * IPRT - HTTP communication API.
5 */
6
7/*
8 * Copyright (C) 2012 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.alldomusa.eu.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 *
18 * The contents of this file may alternatively be used under the terms
19 * of the Common Development and Distribution License Version 1.0
20 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
21 * VirtualBox OSE distribution, in which case the provisions of the
22 * CDDL are applicable instead of those of the GPL.
23 *
24 * You may elect to license modified versions of this file under the
25 * terms and conditions of either the GPL or the CDDL or both.
26 */
27
28
29/*******************************************************************************
30* Header Files *
31*******************************************************************************/
32#include <iprt/http.h>
33#include <iprt/assert.h>
34#include <iprt/err.h>
35#include <iprt/mem.h>
36#include <iprt/string.h>
37
38#include <curl/curl.h>
39#include "internal/magics.h"
40
41
42/*******************************************************************************
43* Structures and Typedefs *
44*******************************************************************************/
45typedef struct RTHTTPINTERNAL
46{
47 uint32_t u32Magic;
48 CURL *pCurl;
49 long lLastResp;
50} RTHTTPINTERNAL;
51typedef RTHTTPINTERNAL *PRTHTTPINTERNAL;
52
53typedef struct RTHTTPMEMCHUNK
54{
55 char *pszMem;
56 size_t cb;
57} RTHTTPMEMCHUNK;
58typedef RTHTTPMEMCHUNK *PRTHTTPMEMCHUNK;
59
60/*******************************************************************************
61* Defined Constants And Macros *
62*******************************************************************************/
63#define CURL_FAILED(rcCurl) (RT_UNLIKELY(rcCurl != CURLE_OK))
64
65/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
66#define RTHTTP_VALID_RETURN_RC(hHttp, rcCurl) \
67 do { \
68 AssertPtrReturn((hHttp), (rcCurl)); \
69 AssertReturn((hHttp)->u32Magic == RTHTTP_MAGIC, (rcCurl)); \
70 } while (0)
71
72/** Validates a handle and returns VERR_INVALID_HANDLE if not valid. */
73#define RTHTTP_VALID_RETURN(hHTTP) RTHTTP_VALID_RETURN_RC((hHttp), VERR_INVALID_HANDLE)
74
75/** Validates a handle and returns (void) if not valid. */
76#define RTHTTP_VALID_RETURN_VOID(hHttp) \
77 do { \
78 AssertPtrReturnVoid(hHttp); \
79 AssertReturnVoid((hHttp)->u32Magic == RTHTTP_MAGIC); \
80 } while (0)
81
82
83RTR3DECL(int) RTHttpCreate(PRTHTTP phHttp)
84{
85 AssertPtrReturn(phHttp, VERR_INVALID_PARAMETER);
86
87 CURLcode rcCurl = curl_global_init(CURL_GLOBAL_ALL);
88 if (CURL_FAILED(rcCurl))
89 return VERR_INTERNAL_ERROR;
90
91 CURL* pCurl = curl_easy_init();
92 if (!pCurl)
93 return VERR_INTERNAL_ERROR;
94
95 PRTHTTPINTERNAL pHttpInt = (PRTHTTPINTERNAL)RTMemAllocZ(sizeof(RTHTTPINTERNAL));
96 if (!pHttpInt)
97 return VERR_NO_MEMORY;
98
99 pHttpInt->u32Magic = RTHTTP_MAGIC;
100 pHttpInt->pCurl = pCurl;
101
102 *phHttp = (RTHTTP)pHttpInt;
103
104 return VINF_SUCCESS;
105}
106
107RTR3DECL(void) RTHttpDestroy(RTHTTP hHttp)
108{
109 if (!hHttp)
110 return;
111
112 PRTHTTPINTERNAL pHttpInt = hHttp;
113 RTHTTP_VALID_RETURN_VOID(pHttpInt);
114
115 pHttpInt->u32Magic = RTHTTP_MAGIC_DEAD;
116
117 curl_easy_cleanup(pHttpInt->pCurl);
118
119 RTMemFree(pHttpInt);
120
121 curl_global_cleanup();
122}
123
124static size_t rtHttpWriteData(void *pvBuf, size_t cb, size_t n, void *pvUser)
125{
126 PRTHTTPMEMCHUNK pMem = (PRTHTTPMEMCHUNK)pvUser;
127 size_t cbAll = cb * n;
128
129 pMem->pszMem = (char*)RTMemRealloc(pMem->pszMem, pMem->cb + cbAll + 1);
130 if (pMem->pszMem)
131 {
132 memcpy(&pMem->pszMem[pMem->cb], pvBuf, cbAll);
133 pMem->cb += cbAll;
134 pMem->pszMem[pMem->cb] = '\0';
135 }
136 return cbAll;
137}
138
139RTR3DECL(int) RTHttpGet(RTHTTP hHttp, const char *pcszUrl, char **ppszResponse)
140{
141 PRTHTTPINTERNAL pHttpInt = hHttp;
142 RTHTTP_VALID_RETURN(pHttpInt);
143
144 int rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_URL, pcszUrl);
145 if (CURL_FAILED(rcCurl))
146 return VERR_INVALID_PARAMETER;
147
148 /* XXX */
149 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt");
150 if (CURL_FAILED(rcCurl))
151 return VERR_INTERNAL_ERROR;
152
153 RTHTTPMEMCHUNK chunk = { NULL, 0 };
154 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_WRITEFUNCTION, &rtHttpWriteData);
155 if (CURL_FAILED(rcCurl))
156 return VERR_INTERNAL_ERROR;
157 rcCurl = curl_easy_setopt(pHttpInt->pCurl, CURLOPT_WRITEDATA, (void*)&chunk);
158 if (CURL_FAILED(rcCurl))
159 return VERR_INTERNAL_ERROR;
160
161 rcCurl = curl_easy_perform(pHttpInt->pCurl);
162 int rc = VERR_INTERNAL_ERROR;
163 if (rcCurl == CURLE_OK)
164 {
165 curl_easy_getinfo(pHttpInt->pCurl, CURLINFO_RESPONSE_CODE, &pHttpInt->lLastResp);
166 switch (pHttpInt->lLastResp)
167 {
168 case 200:
169 /* OK, request was fulfilled */
170 case 204:
171 /* empty response */
172 rc = VINF_SUCCESS;
173 break;
174 case 400:
175 /* bad request */
176 rc = VERR_HTTP_BAD_REQUEST;
177 break;
178 case 403:
179 /* forbidden, authorization will not help */
180 rc = VERR_HTTP_ACCESS_DENIED;
181 break;
182 case 404:
183 /* URL not found */
184 rc = VERR_HTTP_NOT_FOUND;
185 break;
186 }
187 }
188 else
189 {
190 switch (rcCurl)
191 {
192 case CURLE_URL_MALFORMAT:
193 case CURLE_COULDNT_RESOLVE_HOST:
194 rc = VERR_HTTP_NOT_FOUND;
195 break;
196 default:
197 break;
198 }
199 }
200
201 *ppszResponse = chunk.pszMem;
202
203 return rc;
204}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette