All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WebRequestHandle.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Web.h"
6 #include "Cache.h"
7 #include "ICallback.h"
8 #include "Types.h"
9 
10 #include <curl.h>
11 #include <string>
12 
13 namespace Eegeo
14 {
15  namespace Web
16  {
17  namespace WebHandleCompletionCode
18  {
19  enum Values
20  {
21  // Equivalent to HTTP 200 OK, full response returned.
22  Success,
23 
24  // Equivalent to HTTP 304 Not Modified in response to conditional get.
25  NotModified,
26 
27  // Request failed.
28  Failed,
29 
30  Cancelled
31  };
32  }
33 
35  {
36  WebHandleCompletionCode::Values CompletionCode;
37  bool CanFallBackToCache;
38  bool RetryPossible;
39  int HttpResponseCode;
40 
42  : CompletionCode(WebHandleCompletionCode::Failed)
43  , CanFallBackToCache(false)
44  , RetryPossible(false)
45  , HttpResponseCode(0)
46  {}
47  };
48 
49  typedef int TWebRequestHandleId;
50 
52  {
53  public:
54  WebRequestHandle(const TWebRequestHandleId handleId, const std::string& caCertFilePath);
56 
57  CURL* GetCurlHandle() const;
58  bool IsInUse() const;
59  std::string GetUrl() const;
60  bool IsCancelled() const;
61 
62  // mutable
63  void Use(InFlightWebRequest* pWebLoadRequest);
64 
65  void AppendBodyData(Byte* data, size_t size);
66  void AppendHeaderData(Byte* data, size_t size);
67 
68  HandleCompletedResult HandleCompleted(CURLcode curlTransferReturnCode);
69 
70  InFlightWebRequest* GetInFlightWebRequest();
71  TWebRequestHandleId GetHandleId() const;
72  void Reset();
73 
74  private:
75 
76  void SetupCurlOptions();
77  void WritePostData();
78  void WriteMultipartFormData();
79  void WriteMultipartFormField(curl_httppost** httpPost, const std::string& fieldName, const WebPostData& fieldData);
80  void WriteMultipartFormText(curl_httppost** httpPost, const std::string& fieldName, const WebPostData& fieldData);
81  void WriteMultipartFormBinary(curl_httppost** httpPost, const std::string& fieldName, const WebPostData& fieldData);
82  void WriteHeaders();
83 
84  static size_t ReadCallback(void *ptr, size_t size, size_t nmemb, void *stream);
85 
86  bool ValidateHeaders();
87 
88  const TWebRequestHandleId m_handleId;
89  CURL* m_pHandle;
90  curl_slist* m_pCurlHeaderChunk;
91  curl_httppost* m_pCurlFormPost;
92 
93  InFlightWebRequest* m_pInFlightWebRequest;
94 
95  std::string m_caCertFilePath;
96  bool m_caCertRequired;
97  };
98  }
99 }