1 /** @file
2   This file defines the EFI HTTP Protocol interface. It is split into
3   the following two main sections:
4   HTTP Service Binding Protocol (HTTPSB)
5   HTTP Protocol (HTTP)
6 
7   Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>
8   (C) Copyright 2015-2017 Hewlett Packard Enterprise Development LP<BR>
9   SPDX-License-Identifier: BSD-2-Clause-Patent
10 
11   @par Revision Reference:
12   This Protocol is introduced in UEFI Specification 2.5
13 
14 **/
15 
16 #ifndef __EFI_HTTP_PROTOCOL_H__
17 #define __EFI_HTTP_PROTOCOL_H__
18 
19 #define EFI_HTTP_SERVICE_BINDING_PROTOCOL_GUID \
20   { \
21     0xbdc8e6af, 0xd9bc, 0x4379, {0xa7, 0x2a, 0xe0, 0xc4, 0xe7, 0x5d, 0xae, 0x1c } \
22   }
23 
24 #define EFI_HTTP_PROTOCOL_GUID \
25   { \
26     0x7a59b29b, 0x910b, 0x4171, {0x82, 0x42, 0xa8, 0x5a, 0x0d, 0xf2, 0x5b, 0x5b } \
27   }
28 
29 typedef struct _EFI_HTTP_PROTOCOL EFI_HTTP_PROTOCOL;
30 
31 ///
32 /// EFI_HTTP_VERSION
33 ///
34 typedef enum {
35   HttpVersion10,
36   HttpVersion11,
37   HttpVersionUnsupported
38 } EFI_HTTP_VERSION;
39 
40 ///
41 /// EFI_HTTP_METHOD
42 ///
43 typedef enum {
44   HttpMethodGet,
45   HttpMethodPost,
46   HttpMethodPatch,
47   HttpMethodOptions,
48   HttpMethodConnect,
49   HttpMethodHead,
50   HttpMethodPut,
51   HttpMethodDelete,
52   HttpMethodTrace,
53   HttpMethodMax
54 } EFI_HTTP_METHOD;
55 
56 ///
57 /// EFI_HTTP_STATUS_CODE
58 ///
59 typedef enum {
60   HTTP_STATUS_UNSUPPORTED_STATUS = 0,
61   HTTP_STATUS_100_CONTINUE,
62   HTTP_STATUS_101_SWITCHING_PROTOCOLS,
63   HTTP_STATUS_200_OK,
64   HTTP_STATUS_201_CREATED,
65   HTTP_STATUS_202_ACCEPTED,
66   HTTP_STATUS_203_NON_AUTHORITATIVE_INFORMATION,
67   HTTP_STATUS_204_NO_CONTENT,
68   HTTP_STATUS_205_RESET_CONTENT,
69   HTTP_STATUS_206_PARTIAL_CONTENT,
70   HTTP_STATUS_300_MULTIPLE_CHOICES,
71   HTTP_STATUS_301_MOVED_PERMANENTLY,
72   HTTP_STATUS_302_FOUND,
73   HTTP_STATUS_303_SEE_OTHER,
74   HTTP_STATUS_304_NOT_MODIFIED,
75   HTTP_STATUS_305_USE_PROXY,
76   HTTP_STATUS_307_TEMPORARY_REDIRECT,
77   HTTP_STATUS_400_BAD_REQUEST,
78   HTTP_STATUS_401_UNAUTHORIZED,
79   HTTP_STATUS_402_PAYMENT_REQUIRED,
80   HTTP_STATUS_403_FORBIDDEN,
81   HTTP_STATUS_404_NOT_FOUND,
82   HTTP_STATUS_405_METHOD_NOT_ALLOWED,
83   HTTP_STATUS_406_NOT_ACCEPTABLE,
84   HTTP_STATUS_407_PROXY_AUTHENTICATION_REQUIRED,
85   HTTP_STATUS_408_REQUEST_TIME_OUT,
86   HTTP_STATUS_409_CONFLICT,
87   HTTP_STATUS_410_GONE,
88   HTTP_STATUS_411_LENGTH_REQUIRED,
89   HTTP_STATUS_412_PRECONDITION_FAILED,
90   HTTP_STATUS_413_REQUEST_ENTITY_TOO_LARGE,
91   HTTP_STATUS_414_REQUEST_URI_TOO_LARGE,
92   HTTP_STATUS_415_UNSUPPORTED_MEDIA_TYPE,
93   HTTP_STATUS_416_REQUESTED_RANGE_NOT_SATISFIED,
94   HTTP_STATUS_417_EXPECTATION_FAILED,
95   HTTP_STATUS_500_INTERNAL_SERVER_ERROR,
96   HTTP_STATUS_501_NOT_IMPLEMENTED,
97   HTTP_STATUS_502_BAD_GATEWAY,
98   HTTP_STATUS_503_SERVICE_UNAVAILABLE,
99   HTTP_STATUS_504_GATEWAY_TIME_OUT,
100   HTTP_STATUS_505_HTTP_VERSION_NOT_SUPPORTED,
101   HTTP_STATUS_308_PERMANENT_REDIRECT
102 } EFI_HTTP_STATUS_CODE;
103 
104 ///
105 /// EFI_HTTPv4_ACCESS_POINT
106 ///
107 typedef struct {
108   ///
109   /// Set to TRUE to instruct the EFI HTTP instance to use the default address
110   /// information in every TCP connection made by this instance. In addition, when set
111   /// to TRUE, LocalAddress and LocalSubnet are ignored.
112   ///
113   BOOLEAN                       UseDefaultAddress;
114   ///
115   /// If UseDefaultAddress is set to FALSE, this defines the local IP address to be
116   /// used in every TCP connection opened by this instance.
117   ///
118   EFI_IPv4_ADDRESS              LocalAddress;
119   ///
120   /// If UseDefaultAddress is set to FALSE, this defines the local subnet to be used
121   /// in every TCP connection opened by this instance.
122   ///
123   EFI_IPv4_ADDRESS              LocalSubnet;
124   ///
125   /// This defines the local port to be used in
126   /// every TCP connection opened by this instance.
127   ///
128   UINT16                        LocalPort;
129 } EFI_HTTPv4_ACCESS_POINT;
130 
131 ///
132 /// EFI_HTTPv6_ACCESS_POINT
133 ///
134 typedef struct {
135   ///
136   /// Local IP address to be used in every TCP connection opened by this instance.
137   ///
138   EFI_IPv6_ADDRESS              LocalAddress;
139   ///
140   /// Local port to be used in every TCP connection opened by this instance.
141   ///
142   UINT16                        LocalPort;
143 } EFI_HTTPv6_ACCESS_POINT;
144 
145 ///
146 /// EFI_HTTP_CONFIG_DATA_ACCESS_POINT
147 ///
148 
149 
150 typedef struct {
151   ///
152   /// HTTP version that this instance will support.
153   ///
154   EFI_HTTP_VERSION                   HttpVersion;
155   ///
156   /// Time out (in milliseconds) when blocking for requests.
157   ///
158   UINT32                             TimeOutMillisec;
159   ///
160   /// Defines behavior of EFI DNS and TCP protocols consumed by this instance. If
161   /// FALSE, this instance will use EFI_DNS4_PROTOCOL and EFI_TCP4_PROTOCOL. If TRUE,
162   /// this instance will use EFI_DNS6_PROTOCOL and EFI_TCP6_PROTOCOL.
163   ///
164   BOOLEAN                            LocalAddressIsIPv6;
165 
166   union {
167     ///
168     /// When LocalAddressIsIPv6 is FALSE, this points to the local address, subnet, and
169     /// port used by the underlying TCP protocol.
170     ///
171     EFI_HTTPv4_ACCESS_POINT          *IPv4Node;
172     ///
173     /// When LocalAddressIsIPv6 is TRUE, this points to the local IPv6 address and port
174     /// used by the underlying TCP protocol.
175     ///
176     EFI_HTTPv6_ACCESS_POINT          *IPv6Node;
177   } AccessPoint;
178 } EFI_HTTP_CONFIG_DATA;
179 
180 ///
181 /// EFI_HTTP_REQUEST_DATA
182 ///
183 typedef struct {
184   ///
185   /// The HTTP method (e.g. GET, POST) for this HTTP Request.
186   ///
187   EFI_HTTP_METHOD               Method;
188   ///
189   /// The URI of a remote host. From the information in this field, the HTTP instance
190   /// will be able to determine whether to use HTTP or HTTPS and will also be able to
191   /// determine the port number to use. If no port number is specified, port 80 (HTTP)
192   /// is assumed. See RFC 3986 for more details on URI syntax.
193   ///
194   CHAR16                        *Url;
195 } EFI_HTTP_REQUEST_DATA;
196 
197 ///
198 /// EFI_HTTP_RESPONSE_DATA
199 ///
200 typedef struct {
201   ///
202   /// Response status code returned by the remote host.
203   ///
204   EFI_HTTP_STATUS_CODE          StatusCode;
205 } EFI_HTTP_RESPONSE_DATA;
206 
207 ///
208 /// EFI_HTTP_HEADER
209 ///
210 typedef struct {
211   ///
212   /// Null terminated string which describes a field name. See RFC 2616 Section 14 for
213   /// detailed information about field names.
214   ///
215   CHAR8                         *FieldName;
216   ///
217   /// Null terminated string which describes the corresponding field value. See RFC 2616
218   /// Section 14 for detailed information about field values.
219   ///
220   CHAR8                         *FieldValue;
221 } EFI_HTTP_HEADER;
222 
223 ///
224 /// EFI_HTTP_MESSAGE
225 ///
226 typedef struct {
227   ///
228   /// HTTP message data.
229   ///
230   union {
231     ///
232     /// When the token is used to send a HTTP request, Request is a pointer to storage that
233     /// contains such data as URL and HTTP method.
234     ///
235     EFI_HTTP_REQUEST_DATA       *Request;
236     ///
237     /// When used to await a response, Response points to storage containing HTTP response
238     /// status code.
239     ///
240     EFI_HTTP_RESPONSE_DATA      *Response;
241   } Data;
242   ///
243   /// Number of HTTP header structures in Headers list. On request, this count is
244   /// provided by the caller. On response, this count is provided by the HTTP driver.
245   ///
246   UINTN                         HeaderCount;
247   ///
248   /// Array containing list of HTTP headers. On request, this array is populated by the
249   /// caller. On response, this array is allocated and populated by the HTTP driver. It
250   /// is the responsibility of the caller to free this memory on both request and
251   /// response.
252   ///
253   EFI_HTTP_HEADER               *Headers;
254   ///
255   /// Length in bytes of the HTTP body. This can be zero depending on the HttpMethod type.
256   ///
257   UINTN                         BodyLength;
258   ///
259   /// Body associated with the HTTP request or response. This can be NULL depending on
260   /// the HttpMethod type.
261   ///
262   VOID                          *Body;
263 } EFI_HTTP_MESSAGE;
264 
265 
266 ///
267 /// EFI_HTTP_TOKEN
268 ///
269 typedef struct {
270   ///
271   /// This Event will be signaled after the Status field is updated by the EFI HTTP
272   /// Protocol driver. The type of Event must be EFI_NOTIFY_SIGNAL. The Task Priority
273   /// Level (TPL) of Event must be lower than or equal to TPL_CALLBACK.
274   ///
275   EFI_EVENT                     Event;
276   ///
277   /// Status will be set to one of the following value if the HTTP request is
278   /// successfully sent or if an unexpected error occurs:
279   ///   EFI_SUCCESS:      The HTTP request was successfully sent to the remote host.
280   ///   EFI_HTTP_ERROR:   The response message was successfully received but contains a
281   ///                     HTTP error. The response status code is returned in token.
282   ///   EFI_ABORTED:      The HTTP request was cancelled by the caller and removed from
283   ///                     the transmit queue.
284   ///   EFI_TIMEOUT:      The HTTP request timed out before reaching the remote host.
285   ///   EFI_DEVICE_ERROR: An unexpected system or network error occurred.
286   ///
287   EFI_STATUS                    Status;
288   ///
289   /// Pointer to storage containing HTTP message data.
290   ///
291   EFI_HTTP_MESSAGE              *Message;
292 } EFI_HTTP_TOKEN;
293 
294 /**
295   Returns the operational parameters for the current HTTP child instance.
296 
297   The GetModeData() function is used to read the current mode data (operational
298   parameters) for this HTTP protocol instance.
299 
300   @param[in]  This                Pointer to EFI_HTTP_PROTOCOL instance.
301   @param[out] HttpConfigData      Point to buffer for operational parameters of this
302                                   HTTP instance. It is the responsibility of the caller
303                                   to allocate the memory for HttpConfigData and
304                                   HttpConfigData->AccessPoint.IPv6Node/IPv4Node. In fact,
305                                   it is recommended to allocate sufficient memory to record
306                                   IPv6Node since it is big enough for all possibilities.
307 
308   @retval EFI_SUCCESS             Operation succeeded.
309   @retval EFI_INVALID_PARAMETER   This is NULL.
310                                   HttpConfigData is NULL.
311                                   HttpConfigData->AccessPoint.IPv4Node or
312                                   HttpConfigData->AccessPoint.IPv6Node is NULL.
313   @retval EFI_NOT_STARTED         This EFI HTTP Protocol instance has not been started.
314 **/
315 typedef
316 EFI_STATUS
317 (EFIAPI *EFI_HTTP_GET_MODE_DATA)(
318   IN  EFI_HTTP_PROTOCOL         *This,
319   OUT EFI_HTTP_CONFIG_DATA      *HttpConfigData
320   );
321 
322 /**
323   Initialize or brutally reset the operational parameters for this EFI HTTP instance.
324 
325   The Configure() function does the following:
326   When HttpConfigData is not NULL Initialize this EFI HTTP instance by configuring
327   timeout, local address, port, etc.
328   When HttpConfigData is NULL, reset this EFI HTTP instance by closing all active
329   connections with remote hosts, canceling all asynchronous tokens, and flush request
330   and response buffers without informing the appropriate hosts.
331 
332   No other EFI HTTP function can be executed by this instance until the Configure()
333   function is executed and returns successfully.
334 
335   @param[in]  This                Pointer to EFI_HTTP_PROTOCOL instance.
336   @param[in]  HttpConfigData      Pointer to the configure data to configure the instance.
337 
338   @retval EFI_SUCCESS             Operation succeeded.
339   @retval EFI_INVALID_PARAMETER   One or more of the following conditions is TRUE:
340                                   This is NULL.
341                                   HttpConfigData->LocalAddressIsIPv6 is FALSE and
342                                   HttpConfigData->AccessPoint.IPv4Node is NULL.
343                                   HttpConfigData->LocalAddressIsIPv6 is TRUE and
344                                   HttpConfigData->AccessPoint.IPv6Node is NULL.
345   @retval EFI_ALREADY_STARTED     Reinitialize this HTTP instance without calling
346                                   Configure() with NULL to reset it.
347   @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred.
348   @retval EFI_OUT_OF_RESOURCES    Could not allocate enough system resources when
349                                   executing Configure().
350   @retval EFI_UNSUPPORTED         One or more options in ConfigData are not supported
351                                   in the implementation.
352 **/
353 typedef
354 EFI_STATUS
355 (EFIAPI *EFI_HTTP_CONFIGURE)(
356   IN  EFI_HTTP_PROTOCOL         *This,
357   IN  EFI_HTTP_CONFIG_DATA      *HttpConfigData OPTIONAL
358   );
359 
360 /**
361   The Request() function queues an HTTP request to this HTTP instance,
362   similar to Transmit() function in the EFI TCP driver. When the HTTP request is sent
363   successfully, or if there is an error, Status in token will be updated and Event will
364   be signaled.
365 
366   @param[in]  This                Pointer to EFI_HTTP_PROTOCOL instance.
367   @param[in]  Token               Pointer to storage containing HTTP request token.
368 
369   @retval EFI_SUCCESS             Outgoing data was processed.
370   @retval EFI_NOT_STARTED         This EFI HTTP Protocol instance has not been started.
371   @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred.
372   @retval EFI_TIMEOUT             Data was dropped out of the transmit or receive queue.
373   @retval EFI_INVALID_PARAMETER   One or more of the following conditions is TRUE:
374                                   This is NULL.
375                                   Token is NULL.
376                                   Token->Message is NULL.
377                                   Token->Message->Body is not NULL,
378                                   Token->Message->BodyLength is non-zero, and
379                                   Token->Message->Data is NULL, but a previous call to
380                                   Request()has not been completed successfully.
381   @retval EFI_OUT_OF_RESOURCES    Could not allocate enough system resources.
382   @retval EFI_UNSUPPORTED         The HTTP method is not supported in current implementation.
383 **/
384 typedef
385 EFI_STATUS
386 (EFIAPI *EFI_HTTP_REQUEST) (
387   IN  EFI_HTTP_PROTOCOL         *This,
388   IN  EFI_HTTP_TOKEN            *Token
389   );
390 
391 /**
392   Abort an asynchronous HTTP request or response token.
393 
394   The Cancel() function aborts a pending HTTP request or response transaction. If
395   Token is not NULL and the token is in transmit or receive queues when it is being
396   cancelled, its Token->Status will be set to EFI_ABORTED and then Token->Event will
397   be signaled. If the token is not in one of the queues, which usually means that the
398   asynchronous operation has completed, EFI_NOT_FOUND is returned. If Token is NULL,
399   all asynchronous tokens issued by Request() or Response() will be aborted.
400 
401   @param[in]  This                Pointer to EFI_HTTP_PROTOCOL instance.
402   @param[in]  Token               Point to storage containing HTTP request or response
403                                   token.
404 
405   @retval EFI_SUCCESS             Request and Response queues are successfully flushed.
406   @retval EFI_INVALID_PARAMETER   This is NULL.
407   @retval EFI_NOT_STARTED         This instance hasn't been configured.
408   @retval EFI_NOT_FOUND           The asynchronous request or response token is not
409                                   found.
410   @retval EFI_UNSUPPORTED         The implementation does not support this function.
411 **/
412 typedef
413 EFI_STATUS
414 (EFIAPI *EFI_HTTP_CANCEL)(
415   IN  EFI_HTTP_PROTOCOL         *This,
416   IN  EFI_HTTP_TOKEN            *Token
417   );
418 
419 /**
420   The Response() function queues an HTTP response to this HTTP instance, similar to
421   Receive() function in the EFI TCP driver. When the HTTP Response is received successfully,
422   or if there is an error, Status in token will be updated and Event will be signaled.
423 
424   The HTTP driver will queue a receive token to the underlying TCP instance. When data
425   is received in the underlying TCP instance, the data will be parsed and Token will
426   be populated with the response data. If the data received from the remote host
427   contains an incomplete or invalid HTTP header, the HTTP driver will continue waiting
428   (asynchronously) for more data to be sent from the remote host before signaling
429   Event in Token.
430 
431   It is the responsibility of the caller to allocate a buffer for Body and specify the
432   size in BodyLength. If the remote host provides a response that contains a content
433   body, up to BodyLength bytes will be copied from the receive buffer into Body and
434   BodyLength will be updated with the amount of bytes received and copied to Body. This
435   allows the client to download a large file in chunks instead of into one contiguous
436   block of memory. Similar to HTTP request, if Body is not NULL and BodyLength is
437   non-zero and all other fields are NULL or 0, the HTTP driver will queue a receive
438   token to underlying TCP instance. If data arrives in the receive buffer, up to
439   BodyLength bytes of data will be copied to Body. The HTTP driver will then update
440   BodyLength with the amount of bytes received and copied to Body.
441 
442   If the HTTP driver does not have an open underlying TCP connection with the host
443   specified in the response URL, Request() will return EFI_ACCESS_DENIED. This is
444   consistent with RFC 2616 recommendation that HTTP clients should attempt to maintain
445   an open TCP connection between client and host.
446 
447   @param[in]  This                Pointer to EFI_HTTP_PROTOCOL instance.
448   @param[in]  Token               Pointer to storage containing HTTP response token.
449 
450   @retval EFI_SUCCESS             Allocation succeeded.
451   @retval EFI_NOT_STARTED         This EFI HTTP Protocol instance has not been
452                                   initialized.
453   @retval EFI_INVALID_PARAMETER   One or more of the following conditions is TRUE:
454                                   This is NULL.
455                                   Token is NULL.
456                                   Token->Message->Headers is NULL.
457                                   Token->Message is NULL.
458                                   Token->Message->Body is not NULL,
459                                   Token->Message->BodyLength is non-zero, and
460                                   Token->Message->Data is NULL, but a previous call to
461                                   Response() has not been completed successfully.
462   @retval EFI_OUT_OF_RESOURCES    Could not allocate enough system resources.
463   @retval EFI_ACCESS_DENIED       An open TCP connection is not present with the host
464                                   specified by response URL.
465 **/
466 typedef
467 EFI_STATUS
468 (EFIAPI *EFI_HTTP_RESPONSE) (
469   IN  EFI_HTTP_PROTOCOL         *This,
470   IN  EFI_HTTP_TOKEN            *Token
471   );
472 
473 /**
474   The Poll() function can be used by network drivers and applications to increase the
475   rate that data packets are moved between the communication devices and the transmit
476   and receive queues.
477 
478   In some systems, the periodic timer event in the managed network driver may not poll
479   the underlying communications device fast enough to transmit and/or receive all data
480   packets without missing incoming packets or dropping outgoing packets. Drivers and
481   applications that are experiencing packet loss should try calling the Poll() function
482   more often.
483 
484   @param[in]  This                Pointer to EFI_HTTP_PROTOCOL instance.
485 
486   @retval EFI_SUCCESS             Incoming or outgoing data was processed..
487   @retval EFI_DEVICE_ERROR        An unexpected system or network error occurred
488   @retval EFI_INVALID_PARAMETER   This is NULL.
489   @retval EFI_NOT_READY           No incoming or outgoing data is processed.
490   @retval EFI_NOT_STARTED         This EFI HTTP Protocol instance has not been started.
491 **/
492 typedef
493 EFI_STATUS
494 (EFIAPI *EFI_HTTP_POLL) (
495   IN  EFI_HTTP_PROTOCOL         *This
496   );
497 
498 ///
499 /// The EFI HTTP protocol is designed to be used by EFI drivers and applications to
500 /// create and transmit HTTP Requests, as well as handle HTTP responses that are
501 /// returned by a remote host. This EFI protocol uses and relies on an underlying EFI
502 /// TCP protocol.
503 ///
504 struct _EFI_HTTP_PROTOCOL {
505   EFI_HTTP_GET_MODE_DATA        GetModeData;
506   EFI_HTTP_CONFIGURE            Configure;
507   EFI_HTTP_REQUEST              Request;
508   EFI_HTTP_CANCEL               Cancel;
509   EFI_HTTP_RESPONSE             Response;
510   EFI_HTTP_POLL                 Poll;
511 };
512 
513 extern EFI_GUID gEfiHttpServiceBindingProtocolGuid;
514 extern EFI_GUID gEfiHttpProtocolGuid;
515 
516 #endif
517