EvEmu  0.8.4
11 September 2021
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ImageServerConnection Class Reference

Handles a client connection to the image server. More...

#include "ImageServerConnection.h"

Inheritance diagram for ImageServerConnection:
Collaboration diagram for ImageServerConnection:

Public Member Functions

void Process ()
 
boost::asio::ip::tcp::socket & socket ()
 

Static Public Member Functions

static std::shared_ptr
< ImageServerConnection
create (boost::asio::io_context &io)
 

Private Member Functions

 ImageServerConnection (boost::asio::io_context &io)
 
void ProcessHeaders ()
 
void SendImage ()
 
void NotFound ()
 
void Close ()
 
void Redirect ()
 
void RedirectLocation ()
 
void RedirectFinalize ()
 

Static Private Member Functions

static bool starts_with (std::string &haystack, const char *const needle)
 

Private Attributes

std::string _category
 
uint32 _id
 
uint32 _size
 
std::string _redirectUrl
 
boost::asio::streambuf _buffer
 
boost::asio::ip::tcp::socket _socket
 
std::shared_ptr< std::vector
< char > > 
_imageData
 

Static Private Attributes

static boost::asio::const_buffers_1 _responseOK = boost::asio::buffer("HTTP/1.0 200 OK\r\nContent-Type: image/jpeg\r\n\r\n", 45)
 
static boost::asio::const_buffers_1 _responseNotFound = boost::asio::buffer("HTTP/1.0 404 Not Found\r\n\r\n", 26)
 
static boost::asio::const_buffers_1 _responseRedirectBegin = boost::asio::buffer("HTTP/1.0 301 Moved Permanently\r\nLocation: ", 42)
 
static boost::asio::const_buffers_1 _responseRedirectEnd = boost::asio::buffer("\r\n\r\n", 4)
 

Detailed Description

Handles a client connection to the image server.

Handles exactly one client; does all the protocol related stuff. Very limited HTTP handling.

Author
caytchen
Date
April 2011

Definition at line 41 of file ImageServerConnection.h.

Constructor & Destructor Documentation

ImageServerConnection::ImageServerConnection ( boost::asio::io_context &  io)
private

Definition at line 33 of file ImageServerConnection.cpp.

Referenced by create().

34 : _socket(io),
35 _category(0),
36 _id(0),
37 _size(0),
39 {
40 }
static const char *const FallbackURL
Definition: ImageServer.h:68
boost::asio::ip::tcp::socket _socket

Here is the caller graph for this function:

Member Function Documentation

void ImageServerConnection::Close ( )
private

Definition at line 154 of file ImageServerConnection.cpp.

References _socket.

Referenced by NotFound(), RedirectFinalize(), and SendImage().

155 {
156  _socket.close();
157 }
boost::asio::ip::tcp::socket _socket

Here is the caller graph for this function:

std::shared_ptr< ImageServerConnection > ImageServerConnection::create ( boost::asio::io_context &  io)
static

Definition at line 164 of file ImageServerConnection.cpp.

References ImageServerConnection().

Referenced by ImageServerListener::StartAccept().

165 {
166  return std::shared_ptr<ImageServerConnection>(new ImageServerConnection(io));
167 }
ImageServerConnection(boost::asio::io_context &io)

Here is the call graph for this function:

Here is the caller graph for this function:

void ImageServerConnection::NotFound ( )
private

Definition at line 129 of file ImageServerConnection.cpp.

References _responseNotFound, _socket, and Close().

Referenced by ProcessHeaders().

130 {
131  boost::asio::async_write(_socket, _responseNotFound, boost::asio::transfer_all(), std::bind(&ImageServerConnection::Close, shared_from_this()));
132 }
static boost::asio::const_buffers_1 _responseNotFound
boost::asio::ip::tcp::socket _socket

Here is the call graph for this function:

Here is the caller graph for this function:

void ImageServerConnection::Process ( )

Definition at line 47 of file ImageServerConnection.cpp.

References _buffer, _socket, and ProcessHeaders().

48 {
49  // receive all HTTP headers from the client
50  boost::asio::async_read_until(_socket, _buffer, "\r\n\r\n", std::bind(&ImageServerConnection::ProcessHeaders, shared_from_this()));
51 }
boost::asio::ip::tcp::socket _socket
boost::asio::streambuf _buffer

Here is the call graph for this function:

void ImageServerConnection::ProcessHeaders ( )
private

Definition at line 53 of file ImageServerConnection.cpp.

References _buffer, _category, _id, _imageData, _responseOK, _size, _socket, ImageServer::Categories, ImageServer::CategoryCount, IsCharacterID, IsPlayerItem, NotFound(), Redirect(), SendImage(), sImageServer, sLog, and starts_with().

Referenced by Process().

54 {
55  std::istream stream(&_buffer);
56  std::string request;
57 
58  // every request line ends with \r\n
59  std::getline(stream, request, '\r');
60 
61  if (!starts_with(request, "GET /"))
62  {
63  NotFound();
64  return;
65  }
66  request = request.substr(5);
67 
68  bool found = false;
69  for (uint32 i = 0; i < ImageServer::CategoryCount; i++)
70  {
71  if (starts_with(request, ImageServer::Categories[i]))
72  {
73  found = true;
75  request = request.substr(strlen(ImageServer::Categories[i]));
76  break;
77  }
78  }
79  if (!found)
80  {
81  NotFound();
82  return;
83  }
84 
85  if (!starts_with(request, "/"))
86  {
87  NotFound();
88  return;
89  }
90  request = request.substr(1);
91 
92  int del = request.find_first_of('_');
93  if (del == std::string::npos)
94  {
95  NotFound();
96  return;
97  }
98 
99  // might have some extra data but atoi shouldn't care
100  std::string idStr = request.substr(0, del);
101  std::string sizeStr = request.substr(del + 1);
102  _id = atoi(idStr.c_str());
103  _size = atoi(sizeStr.c_str());
104 
105  _imageData = sImageServer.GetImage(_category, _id, _size);
106  if (!_imageData) {
107  if (IsPlayerItem(_id)) {
108  sLog.Error(" Image Server","Image for itemID %u not found.", _id);
109  NotFound();
110  return;
111  } else if (IsCharacterID(_id)) {
112  sLog.Error(" Image Server","Image for charID %u not found.", _id);
113  NotFound();
114  return;
115  }
116  Redirect();
117  return;
118  }
119 
120  // first we have to send the responseOK, then our actual result
121  boost::asio::async_write(_socket, _responseOK, boost::asio::transfer_all(), std::bind(&ImageServerConnection::SendImage, shared_from_this()));
122 }
static const char *const Categories[]
Definition: ImageServer.h:63
#define sImageServer
Definition: ImageServer.h:93
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
std::shared_ptr< std::vector< char > > _imageData
boost::asio::ip::tcp::socket _socket
#define IsPlayerItem(itemID)
Definition: EVE_Defines.h:256
static const uint32 CategoryCount
Definition: ImageServer.h:64
static boost::asio::const_buffers_1 _responseOK
#define IsCharacterID(itemID)
Definition: EVE_Defines.h:206
unsigned __int32 uint32
Definition: eve-compat.h:50
boost::asio::streambuf _buffer
static bool starts_with(std::string &haystack, const char *const needle)

Here is the call graph for this function:

Here is the caller graph for this function:

void ImageServerConnection::Redirect ( )
private

Definition at line 134 of file ImageServerConnection.cpp.

References _responseRedirectBegin, _socket, and RedirectLocation().

Referenced by ProcessHeaders().

135 {
136  boost::asio::async_write(_socket, _responseRedirectBegin, boost::asio::transfer_all(), std::bind(&ImageServerConnection::RedirectLocation, shared_from_this()));
137 }
boost::asio::ip::tcp::socket _socket
static boost::asio::const_buffers_1 _responseRedirectBegin

Here is the call graph for this function:

Here is the caller graph for this function:

void ImageServerConnection::RedirectFinalize ( )
private

Definition at line 149 of file ImageServerConnection.cpp.

References _responseRedirectEnd, _socket, and Close().

Referenced by RedirectLocation().

150 {
151  boost::asio::async_write(_socket, _responseRedirectEnd, boost::asio::transfer_all(), std::bind(&ImageServerConnection::Close, shared_from_this()));
152 }
static boost::asio::const_buffers_1 _responseRedirectEnd
boost::asio::ip::tcp::socket _socket

Here is the call graph for this function:

Here is the caller graph for this function:

void ImageServerConnection::RedirectLocation ( )
private

Definition at line 139 of file ImageServerConnection.cpp.

References _category, _id, _redirectUrl, _size, _socket, ImageServer::FallbackURL, RedirectFinalize(), and sLog.

Referenced by Redirect().

140 {
141  sLog.Error(" Image Server"," RedirectLocation() called.");
142  std::string extension = _category == "Character" ? "jpg" : "png";
143  std::stringstream url;
144  url << ImageServer::FallbackURL << _category << "/" << _id << "_" << _size << "." << extension;
145  _redirectUrl = url.str();
146  boost::asio::async_write(_socket, boost::asio::buffer(_redirectUrl), boost::asio::transfer_all(), std::bind(&ImageServerConnection::RedirectFinalize, shared_from_this()));
147 }
static const char *const FallbackURL
Definition: ImageServer.h:68
#define sLog
Evaluates to a NewLog instance.
Definition: LogNew.h:250
boost::asio::ip::tcp::socket _socket

Here is the call graph for this function:

Here is the caller graph for this function:

void ImageServerConnection::SendImage ( )
private

Definition at line 124 of file ImageServerConnection.cpp.

References _imageData, _socket, and Close().

Referenced by ProcessHeaders().

125 {
126  boost::asio::async_write(_socket, boost::asio::buffer(*_imageData, _imageData->size()), boost::asio::transfer_all(), std::bind(&ImageServerConnection::Close, shared_from_this()));
127 }
std::shared_ptr< std::vector< char > > _imageData
boost::asio::ip::tcp::socket _socket

Here is the call graph for this function:

Here is the caller graph for this function:

boost::asio::ip::tcp::socket & ImageServerConnection::socket ( )

Definition at line 42 of file ImageServerConnection.cpp.

References _socket.

43 {
44  return _socket;
45 }
boost::asio::ip::tcp::socket _socket
bool ImageServerConnection::starts_with ( std::string &  haystack,
const char *const  needle 
)
staticprivate

Definition at line 159 of file ImageServerConnection.cpp.

Referenced by ProcessHeaders().

160 {
161  return haystack.substr(0, strlen(needle)).compare(needle) == 0;
162 }

Here is the caller graph for this function:

Member Data Documentation

boost::asio::streambuf ImageServerConnection::_buffer
private

Definition at line 66 of file ImageServerConnection.h.

Referenced by Process(), and ProcessHeaders().

std::string ImageServerConnection::_category
private

Definition at line 61 of file ImageServerConnection.h.

Referenced by ProcessHeaders(), and RedirectLocation().

uint32 ImageServerConnection::_id
private

Definition at line 62 of file ImageServerConnection.h.

Referenced by ProcessHeaders(), and RedirectLocation().

std::shared_ptr<std::vector<char> > ImageServerConnection::_imageData
private

Definition at line 68 of file ImageServerConnection.h.

Referenced by ProcessHeaders(), and SendImage().

std::string ImageServerConnection::_redirectUrl
private

Definition at line 64 of file ImageServerConnection.h.

Referenced by RedirectLocation().

boost::asio::const_buffers_1 ImageServerConnection::_responseNotFound = boost::asio::buffer("HTTP/1.0 404 Not Found\r\n\r\n", 26)
staticprivate

Definition at line 71 of file ImageServerConnection.h.

Referenced by NotFound().

boost::asio::const_buffers_1 ImageServerConnection::_responseOK = boost::asio::buffer("HTTP/1.0 200 OK\r\nContent-Type: image/jpeg\r\n\r\n", 45)
staticprivate

Definition at line 70 of file ImageServerConnection.h.

Referenced by ProcessHeaders().

boost::asio::const_buffers_1 ImageServerConnection::_responseRedirectBegin = boost::asio::buffer("HTTP/1.0 301 Moved Permanently\r\nLocation: ", 42)
staticprivate

Definition at line 72 of file ImageServerConnection.h.

Referenced by Redirect().

boost::asio::const_buffers_1 ImageServerConnection::_responseRedirectEnd = boost::asio::buffer("\r\n\r\n", 4)
staticprivate

Definition at line 73 of file ImageServerConnection.h.

Referenced by RedirectFinalize().

uint32 ImageServerConnection::_size
private

Definition at line 63 of file ImageServerConnection.h.

Referenced by ProcessHeaders(), and RedirectLocation().

boost::asio::ip::tcp::socket ImageServerConnection::_socket
private

The documentation for this class was generated from the following files: