00001 /** 00002 Licensed to the Apache Software Foundation (ASF) under one 00003 or more contributor license agreements. See the NOTICE file 00004 distributed with this work for additional information 00005 regarding copyright ownership. The ASF licenses this file 00006 to you under the Apache License, Version 2.0 (the 00007 "License"); you may not use this file except in compliance 00008 with the License. You may obtain a copy of the License at 00009 00010 http://www.apache.org/licenses/LICENSE-2.0 00011 00012 Unless required by applicable law or agreed to in writing, software 00013 distributed under the License is distributed on an "AS IS" BASIS, 00014 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00015 See the License for the specific language governing permissions and 00016 limitations under the License. 00017 */ 00018 00019 /** 00020 * @file AsyncHttpFetch.h 00021 */ 00022 00023 #pragma once 00024 #ifndef ATSCPPAPI_ASYNCHTTPFETCH_H_ 00025 #define ATSCPPAPI_ASYNCHTTPFETCH_H_ 00026 00027 #include <string> 00028 #include <atscppapi/shared_ptr.h> 00029 #include <atscppapi/Async.h> 00030 #include <atscppapi/Request.h> 00031 #include <atscppapi/Response.h> 00032 00033 namespace atscppapi { 00034 00035 // forward declarations 00036 struct AsyncHttpFetchState; 00037 namespace utils { class internal; } 00038 00039 /** 00040 * @brief This class provides an implementation of AsyncProvider that 00041 * makes HTTP requests asynchronously. This provider automatically 00042 * self-destructs after the completion of the request. 00043 * 00044 * See example async_http_fetch for sample usage. 00045 */ 00046 class AsyncHttpFetch : public AsyncProvider { 00047 public: 00048 AsyncHttpFetch(const std::string &url_str, HttpMethod http_method = HTTP_METHOD_GET); 00049 00050 AsyncHttpFetch(const std::string &url_str, const std::string &request_body); 00051 00052 /** 00053 * Used to manipulate the headers of the request to be made. 00054 * 00055 * @return A reference to mutable headers. 00056 */ 00057 Headers &getRequestHeaders(); 00058 00059 enum Result { RESULT_SUCCESS = 10000, RESULT_TIMEOUT, RESULT_FAILURE }; 00060 00061 /** 00062 * Used to extract the response after request completion. 00063 * 00064 * @return Result of the operation 00065 */ 00066 Result getResult() const; 00067 00068 /** 00069 * @return Non-mutable reference to the request URL. 00070 */ 00071 const Url &getRequestUrl() const; 00072 00073 /** 00074 * @return Non-mutable reference to the request body. 00075 */ 00076 const std::string &getRequestBody() const; 00077 00078 /** 00079 * Used to extract the response after request completion. 00080 * 00081 * @return Non-mutable reference to the response. 00082 */ 00083 const Response &getResponse() const; 00084 00085 /** 00086 * Used to extract the body of the response after request completion. On 00087 * unsuccessful completion, values (NULL, 0) are set. 00088 * 00089 * @param body Output argument; will point to the body 00090 * @param body_size Output argument; will contain the size of the body 00091 * 00092 */ 00093 void getResponseBody(const void *&body, size_t &body_size) const; 00094 00095 /** 00096 * Starts a HTTP fetch of the Request contained. 00097 */ 00098 virtual void run(); 00099 protected: 00100 virtual ~AsyncHttpFetch(); 00101 00102 private: 00103 AsyncHttpFetchState *state_; 00104 void init(const std::string &url_str, HttpMethod http_method, const std::string &request_body); 00105 friend class utils::internal; 00106 }; 00107 00108 } /* atscppapi */ 00109 00110 #endif /* ATSCPPAPI_ASYNCHTTPFETCH_H_ */