Add net-lib (HttpRequest/Response classes).

This commit is contained in:
David Capello 2011-06-12 10:50:30 -03:00
parent 68fde07334
commit 92a7b29e60
9 changed files with 359 additions and 5 deletions

View File

@ -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)

View File

@ -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()

7
src/net/CMakeLists.txt Normal file
View File

@ -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)

31
src/net/http_headers.cpp Normal file
View File

@ -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

48
src/net/http_headers.h Normal file
View File

@ -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 <map>
#include <string>
namespace net {
class HttpHeaders
{
public:
typedef std::map<std::string, std::string> 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

125
src/net/http_request.cpp Normal file
View File

@ -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 <curl/curl.h>
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<HttpRequestImpl*>(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

50
src/net/http_request.h Normal file
View File

@ -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 <string>
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

32
src/net/http_response.cpp Normal file
View File

@ -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 <ostream>
namespace net {
void HttpResponse::write(const char* data, size_t length)
{
m_stream->write(data, length);
}
} // namespace net

54
src/net/http_response.h Normal file
View File

@ -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 <iosfwd>
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