From 92a7b29e6047e578949e77426c2c000962dee76e Mon Sep 17 00:00:00 2001 From: David Capello Date: Sun, 12 Jun 2011 10:50:30 -0300 Subject: [PATCH] Add net-lib (HttpRequest/Response classes). --- CMakeLists.txt | 1 + src/CMakeLists.txt | 16 +++-- src/net/CMakeLists.txt | 7 +++ src/net/http_headers.cpp | 31 ++++++++++ src/net/http_headers.h | 48 +++++++++++++++ src/net/http_request.cpp | 125 ++++++++++++++++++++++++++++++++++++++ src/net/http_request.h | 50 +++++++++++++++ src/net/http_response.cpp | 32 ++++++++++ src/net/http_response.h | 54 ++++++++++++++++ 9 files changed, 359 insertions(+), 5 deletions(-) create mode 100644 src/net/CMakeLists.txt create mode 100644 src/net/http_headers.cpp create mode 100644 src/net/http_headers.h create mode 100644 src/net/http_request.cpp create mode 100644 src/net/http_request.h create mode 100644 src/net/http_response.cpp create mode 100644 src/net/http_response.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f56f925b8..0f63b5731 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,6 +65,7 @@ endif(COMPILER_MSVC) ###################################################################### # Directories +set(CURL_DIR ${CMAKE_SOURCE_DIR}/third_party/curl) set(GIFLIB_DIR ${CMAKE_SOURCE_DIR}/third_party/giflib) set(LIBFREETYPE_DIR ${CMAKE_SOURCE_DIR}/third_party/freetype) set(LIBJPEG_DIR ${CMAKE_SOURCE_DIR}/third_party/jpeg) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 767b85e08..90f17d78a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -42,17 +42,22 @@ else() set(libs3rdparty ${libs3rdparty} libpng) endif() +if(CURL_STATICLIB) + add_definitions(-DCURL_STATICLIB) +endif() + if (CMAKE_USE_PTHREADS_INIT) set(sys_libs ${sys_libs} ${CMAKE_THREAD_LIBS_INIT}) endif() # All libraries for .exe files -set(all_libs aseprite-library undo-lib filters-lib gui-lib gfx-lib base-lib +set(all_libs aseprite-library undo-lib filters-lib gui-lib gfx-lib net-lib base-lib ${libs3rdparty} allegro ${sys_libs}) # Directories where .h files can be found include_directories( . .. ../third_party + ${CURL_DIR}/include ${GIFLIB_DIR}/lib ${LIBFREETYPE_DIR}/include ${LIBJPEG_DIR} @@ -66,9 +71,10 @@ include_directories( add_subdirectory(allegro) add_subdirectory(base) +add_subdirectory(filters) add_subdirectory(gfx) add_subdirectory(gui) -add_subdirectory(filters) +add_subdirectory(net) add_subdirectory(undo) ###################################################################### @@ -318,14 +324,14 @@ add_executable(aseprite WIN32 main.cpp ${win32_resources}) target_link_libraries(aseprite ${all_libs}) -INSTALL(TARGETS aseprite +install(TARGETS aseprite RUNTIME DESTINATION bin) -INSTALL(DIRECTORY ../data +install(DIRECTORY ../data DESTINATION share/aseprite) if(EXISTS ../docs/quickref.pdf) - INSTALL(FILES ../docs/quickref.pdf + install(FILES ../docs/quickref.pdf DESTINATION share/aseprite/docs/quickref.pdf) endif() diff --git a/src/net/CMakeLists.txt b/src/net/CMakeLists.txt new file mode 100644 index 000000000..6792d062e --- /dev/null +++ b/src/net/CMakeLists.txt @@ -0,0 +1,7 @@ +# ASE - Allegro Sprite Editor +# Copyright (C) 2001-2011 David Capello + +add_library(net-lib + http_headers.cpp + http_request.cpp + http_response.cpp) diff --git a/src/net/http_headers.cpp b/src/net/http_headers.cpp new file mode 100644 index 000000000..b673dd5b9 --- /dev/null +++ b/src/net/http_headers.cpp @@ -0,0 +1,31 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 David Capello + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#include "net/http_headers.h" + +namespace net { + +void HttpHeaders::setHeader(const std::string& name, + const std::string& value) +{ + m_map[name] = value; +} + +} // namespace net diff --git a/src/net/http_headers.h b/src/net/http_headers.h new file mode 100644 index 000000000..cfe3cd6b8 --- /dev/null +++ b/src/net/http_headers.h @@ -0,0 +1,48 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 David Capello + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef NET_HTTP_HEADERS_H_INCLUDED +#define NET_HTTP_HEADERS_H_INCLUDED + +#include +#include + +namespace net { + +class HttpHeaders +{ +public: + typedef std::map Map; + typedef Map::iterator iterator; + typedef Map::const_iterator const_iterator; + + iterator begin() { return m_map.begin(); } + iterator end() { return m_map.end(); } + const_iterator begin() const { return m_map.begin(); } + const_iterator end() const { return m_map.end(); } + + void setHeader(const std::string& name, + const std::string& value); + +private: + Map m_map; +}; + +} // namespace net + +#endif // NET_HTTP_HEADERS_H_INCLUDED diff --git a/src/net/http_request.cpp b/src/net/http_request.cpp new file mode 100644 index 000000000..ad9d6bafc --- /dev/null +++ b/src/net/http_request.cpp @@ -0,0 +1,125 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 David Capello + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#include "net/http_request.h" + +#include "net/http_headers.h" +#include "net/http_response.h" + +#include + +namespace net { + +class HttpRequestImpl +{ +public: + HttpRequestImpl(const std::string& url) + : m_curl(curl_easy_init()) + , m_headerlist(NULL) + , m_response(NULL) + { + curl_easy_setopt(m_curl, CURLOPT_WRITEDATA, this); + curl_easy_setopt(m_curl, CURLOPT_WRITEFUNCTION, &HttpRequestImpl::writeBodyCallback); + curl_easy_setopt(m_curl, CURLOPT_URL, url.c_str()); + } + + ~HttpRequestImpl() + { + if (m_headerlist) + curl_slist_free_all(m_headerlist); + + curl_easy_cleanup(m_curl); + } + + void setHeaders(const HttpHeaders& headers) + { + if (m_headerlist) { + curl_slist_free_all(m_headerlist); + m_headerlist = NULL; + } + + std::string tmp; + for (HttpHeaders::const_iterator it=headers.begin(), end=headers.end(); it!=end; ++it) { + tmp = it->first; + tmp += ": "; + tmp += it->second; + + m_headerlist = curl_slist_append(m_headerlist, tmp.c_str()); + } + + curl_easy_setopt(m_curl, CURLOPT_HTTPHEADER, m_headerlist); + } + + void send(HttpResponse& response) + { + m_response = &response; + curl_easy_perform(m_curl); + } + + void abort() + { + // TODO + } + +private: + size_t writeBody(char* ptr, size_t bytes) + { + ASSERT(m_response != NULL); + m_response->write(ptr, bytes); + return bytes; + } + + static size_t writeBodyCallback(char* ptr, size_t size, size_t nmemb, void* userdata) + { + HttpRequestImpl* req = reinterpret_cast(userdata); + return req->writeBody(ptr, size*nmemb); + } + + CURL* m_curl; + curl_slist* m_headerlist; + HttpResponse* m_response; +}; + +HttpRequest::HttpRequest(const std::string& url) + : m_impl(new HttpRequestImpl(url)) +{ +} + +HttpRequest::~HttpRequest() +{ + delete m_impl; +} + +void HttpRequest::setHeaders(const HttpHeaders& headers) +{ + m_impl->setHeaders(headers); +} + +void HttpRequest::send(HttpResponse& response) +{ + m_impl->send(response); +} + +void HttpRequest::abort() +{ + m_impl->abort(); +} + +} // namespace net diff --git a/src/net/http_request.h b/src/net/http_request.h new file mode 100644 index 000000000..e7fa5dc8b --- /dev/null +++ b/src/net/http_request.h @@ -0,0 +1,50 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 David Capello + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef NET_HTTP_REQUEST_H_INCLUDED +#define NET_HTTP_REQUEST_H_INCLUDED + +#include "base/disable_copying.h" + +#include + +namespace net { + +class HttpHeaders; +class HttpRequestImpl; +class HttpResponse; + +class HttpRequest +{ +public: + HttpRequest(const std::string& url); + ~HttpRequest(); + + void setHeaders(const HttpHeaders& headers); + void send(HttpResponse& response); + void abort(); + +private: + HttpRequestImpl* m_impl; + + DISABLE_COPYING(HttpRequest); +}; + +} // namespace net + +#endif diff --git a/src/net/http_response.cpp b/src/net/http_response.cpp new file mode 100644 index 000000000..4952faab0 --- /dev/null +++ b/src/net/http_response.cpp @@ -0,0 +1,32 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 David Capello + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include "config.h" + +#include "net/http_response.h" + +#include + +namespace net { + +void HttpResponse::write(const char* data, size_t length) +{ + m_stream->write(data, length); +} + +} // namespace net diff --git a/src/net/http_response.h b/src/net/http_response.h new file mode 100644 index 000000000..8f820df50 --- /dev/null +++ b/src/net/http_response.h @@ -0,0 +1,54 @@ +/* ASE - Allegro Sprite Editor + * Copyright (C) 2001-2011 David Capello + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef NET_HTTP_RESPONSE_H_INCLUDED +#define NET_HTTP_RESPONSE_H_INCLUDED + +#include "base/disable_copying.h" + +#include + +namespace net { + +class HttpResponse +{ +public: + // Creates a response. The body of the response will be written in + // the given "stream". + HttpResponse(std::ostream* stream) + : m_status(0) + , m_stream(stream) + { } + + // Returns the HTTP status code. + int status() const { return m_status; } + void status(int status) { m_status = status; } + + // Writes data in the stream. + void write(const char* data, size_t length); + +private: + int m_status; + std::ostream* m_stream; + + DISABLE_COPYING(HttpResponse); +}; + +} // namespace net + +#endif // NET_HTTP_RESPONSE_H_INCLUDED