-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Muevo la clase de mongoose para establecer la coneccion. #32
- Loading branch information
1 parent
d73fc90
commit 0ca2e3c
Showing
2 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#include "mg_connection.h" | ||
|
||
extern "C" { | ||
#include <stdarg.h> | ||
} | ||
|
||
#include <cstdlib> | ||
#include <cstring> | ||
|
||
using std::atoi; | ||
using std::string; | ||
using std::strlen; | ||
|
||
static const char* CONTENT_TYPES[] = { | ||
"application/json", // CONTENT_TYPE_JSON | ||
"text/html" // CONTENT_TYPE_HTML | ||
}; | ||
|
||
MgConnection::MgConnection(struct mg_connection *c) : conn(c) { | ||
|
||
} | ||
|
||
void MgConnection::sendStatus(MgConnection::StatusCodes code){ | ||
this->sendStatus( (int) code); | ||
} | ||
|
||
void MgConnection::sendStatus(int code){ | ||
mg_send_status(this->conn, code); | ||
} | ||
|
||
void MgConnection::sendHeader(const string& name, const string& val){ | ||
this->sendHeader(name.c_str(), val.c_str()); | ||
} | ||
|
||
void MgConnection::sendHeader(const char* name, const char* val){ | ||
mg_send_header(this->conn, name, val); | ||
} | ||
|
||
size_t MgConnection::printfData(const char* fmt, ...){ | ||
va_list ap; | ||
va_start(ap, fmt); | ||
size_t ret = mg_vprintf_data(this->conn, fmt, ap); | ||
va_end(ap); | ||
return ret; | ||
} | ||
|
||
struct mg_connection* MgConnection::operator->(){ | ||
return this->conn; | ||
} | ||
|
||
void MgConnection::sendContentType(const std::string& type){ | ||
this->sendContentType(type.c_str()); | ||
} | ||
|
||
void MgConnection::sendContentType(const char* type){ | ||
this->sendHeader("Content-Type", type); | ||
} | ||
|
||
void MgConnection::sendContentType(MgConnection::ContentTypes type){ | ||
if(type >= MgConnection::CONTENT_TYPE_TOTAL) | ||
return; | ||
this->sendContentType(CONTENT_TYPES[type]); | ||
} | ||
|
||
const std::string& MgConnection::getParameter(const string& key){ | ||
return this->parameters[key]; | ||
} | ||
|
||
void MgConnection::setParameter(const std::string& key, const string& value){ | ||
this->parameters[key] = value; | ||
} | ||
|
||
string MgConnection::getVarStr(const char* varName, size_t max){ | ||
string value; | ||
value.resize(max); | ||
// XXX: fixme es feo esto!,, jaja | ||
switch(mg_get_var(this->conn, varName, (char*) value.data(), max)){ | ||
case -2: | ||
return this->getVarStr(varName, max+max); | ||
break; | ||
|
||
case -1: | ||
return string(); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
|
||
value.resize(strlen(value.data())); | ||
|
||
return value; | ||
} | ||
|
||
string MgConnection::getVarStr(const string& varName, size_t max){ | ||
return this->getVarStr(varName.c_str(), max); | ||
} | ||
|
||
|
||
int MgConnection::getVarInt(const char* varName, size_t max){ | ||
return atoi(this->getVarStr(varName, max).c_str()); | ||
|
||
} | ||
|
||
int MgConnection::getVarInt(const string& varName, size_t max){ | ||
return this->getVarInt(varName, max); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
#ifndef __MG_CONECTION_H__ | ||
#define __MG_CONECTION_H__ | ||
|
||
#include "mongoose/mongoose.h" | ||
|
||
#include <map> | ||
#include <string> | ||
|
||
/** Clase que representa una coneccion, abstrae struct mg_connection de mongoose | ||
*/ | ||
class MgConnection { | ||
public: | ||
/** Constructor | ||
* @param coneccion | ||
*/ | ||
MgConnection(struct mg_connection *conn); | ||
|
||
/** Enum que representa los codigos de estado de html | ||
*/ | ||
typedef enum StatusCodes { | ||
// 2xx Success | ||
STATUS_CODE_OK=200, ///< Todo bien | ||
STATUS_CODE_CREATED=201, ///< Se creo algo | ||
|
||
// 4xx Client Error | ||
STATUS_CODE_BAD_REQUEST=400, ///< El cliente no paso toda la informacion, request malo | ||
STATUS_CODE_UNAUTHORIZED=401, ///< Cliente no esta autorizado para lo que quiere hacer | ||
STATUS_CODE_FORBIDDEN=403, ///< El cliente tiene prohibido hacer eso (no es un tema de permisos) | ||
STATUS_CODE_NOT_FOUND=404, ///< No se encuentro | ||
STATUS_CODE_METHOD_NOT_ALLOWED=405, ///< Se pidio un GET/POST/DELETE y no se implementa ese metodo | ||
|
||
// 5xx Errores de Servidor | ||
STATUS_CODE_INTERNAL_ERROR=500 ///< Error del servidor | ||
|
||
} StatusCodes; | ||
|
||
/** Enum que representa los content types que puede tener la respuesta | ||
*/ | ||
typedef enum ContentTypes { | ||
CONTENT_TYPE_JSON=0, ///< Se envia un JSON | ||
CONTENT_TYPE_HTML, ///< Se envia un HTML | ||
CONTENT_TYPE_TOTAL | ||
} ContentTypes; | ||
|
||
/** Envia el codigo de estado | ||
* @param codigo (enum) | ||
*/ | ||
void sendStatus(MgConnection::StatusCodes code); | ||
|
||
/** Envia el codigo de estado | ||
* @param codigo entero | ||
*/ | ||
void sendStatus(int code); | ||
|
||
/** Setea un header del request | ||
* @param name: nombre del header | ||
* @param val: valor | ||
*/ | ||
void sendHeader(const std::string& name, const std::string& val); | ||
void sendHeader(const char* name, const char* val); | ||
|
||
/** Setea el content type | ||
*/ | ||
void sendContentType(const std::string& type); | ||
void sendContentType(const char* type); | ||
void sendContentType(MgConnection::ContentTypes type); | ||
|
||
/** Printf - abstrae mg_vprintf_data | ||
*/ | ||
size_t printfData(const char* format, ...); | ||
|
||
/** Operador derreferencia. | ||
* @return puntero a mg_connection | ||
*/ | ||
struct mg_connection* operator->(); | ||
|
||
/** Obtiene un parametro que anteriormente fue guardado para esta coneccion | ||
* @param key: clave por la cual fue guardado | ||
* @return valor del parametro o "" si no existe | ||
*/ | ||
const std::string& getParameter(const std::string& key); | ||
|
||
/** Guarda un parametro para la coneccion | ||
* @param key: clave | ||
* @param value: valor | ||
*/ | ||
void setParameter(const std::string& key, const std::string& value); | ||
|
||
/** Obtiene un parametro de la url o de post (si es url encoded) | ||
* @param varName: nombre del parametro | ||
* @param max: buffer a allocar | ||
* @return valor del parametro o "" si no existe | ||
*/ | ||
std::string getVarStr(const char* varName, size_t max=64); | ||
std::string getVarStr(const std::string& varName, size_t max=64); | ||
|
||
/** Obtiene un parametro de la url, lo convierte a entero | ||
* @see MgConnection::getVarStr | ||
*/ | ||
int getVarInt(const char* varName, size_t max=64); | ||
int getVarInt(const std::string& varName, size_t max=64); | ||
|
||
|
||
protected: | ||
struct mg_connection *conn; ///< Instacia de mg_connection | ||
std::map<std::string, std::string> parameters; ///< map de los parametros guardados por el usuario | ||
}; | ||
|
||
#endif |