Browse Source

"""const correctness"""

pull/1/head
gyroninja 5 years ago
parent
commit
c8ece28a75
  1. 4
      Makefile
  2. 8
      src/graphics/opengl/Box.cpp
  3. 10
      src/graphics/opengl/Box.h
  4. 36
      src/graphics/opengl/Text.cpp
  5. 8
      src/graphics/opengl/Text.h
  6. 34
      src/graphics/opengl/Window.cpp
  7. 8
      src/graphics/opengl/Window.h
  8. 37
      src/graphics/text/TextRasterizer.cpp
  9. 6
      src/graphics/text/TextRasterizer.h
  10. 10
      src/html/HTMLParser.cpp
  11. 4
      src/html/HTMLParser.h
  12. 16
      src/main.cpp
  13. 16
      src/networking/HTTPRequest.cpp
  14. 8
      src/networking/HTTPRequest.h
  15. 2
      src/networking/HTTPResponse.cpp
  16. 2
      src/networking/HTTPResponse.h

4
Makefile

@ -1,8 +1,8 @@ @@ -1,8 +1,8 @@
CXX = g++
CXXFLAGS =
CXXFLAGS = -O3
INCPATH = -I /usr/include/freetype2 -I /usr/include/harfbuzz
LINK = g++
LDFLAGS =
LDFLAGS = -O3 -flto
LIBS = -lglfw -lGL -lGLEW -lfreetype -lharfbuzz
SRCDIR = src
OBJDIR = gen

8
src/graphics/opengl/Box.cpp

@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
#include "../../../anime.h"
#include <cmath>
Box::Box(float x, float y, float width, float height, int windowWidth, int windowHeight) {
Box::Box(const float x, const float y, const float width, const float height, const int windowWidth, const int windowHeight) {
this->x = x;
this->y = y;
this->width = width;
@ -87,7 +87,7 @@ void Box::render() { @@ -87,7 +87,7 @@ void Box::render() {
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
}
void Box::resize(int windowWidth, int windowHeight) {
void Box::resize(const int windowWidth, const int windowHeight) {
float vx = x;
float vy = y;
float vWidth = width;
@ -107,7 +107,7 @@ void Box::resize(int windowWidth, int windowHeight) { @@ -107,7 +107,7 @@ void Box::resize(int windowWidth, int windowHeight) {
verticesDirty = true;
}
void Box::pointToViewport(float &x, float &y, int windowWidth, int windowHeight) {
void Box::pointToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const {
if (x < 0) {
x += windowWidth;
}
@ -124,7 +124,7 @@ void Box::pointToViewport(float &x, float &y, int windowWidth, int windowHeight) @@ -124,7 +124,7 @@ void Box::pointToViewport(float &x, float &y, int windowWidth, int windowHeight)
y = (y * 2) - 1;
}
void Box::distanceToViewport(float &x, float &y, int windowWidth, int windowHeight) {
void Box::distanceToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const {
if (std::abs(x) > 1) {
x /= windowWidth;
}

10
src/graphics/opengl/Box.h

@ -16,7 +16,7 @@ private: @@ -16,7 +16,7 @@ private:
0.0f, 0.0f, 0.0f, 0.0f, 0.0f
};
bool verticesDirty = false;
unsigned int indices[6] = {
const unsigned int indices[6] = {
0, 1, 2,
0, 2, 3
};
@ -26,12 +26,12 @@ private: @@ -26,12 +26,12 @@ private:
GLuint elementBufferObject = 0;
GLuint texture = 0;
public:
Box(float x, float y, float width, float height, int windowWidth, int windowHeight);
Box(const float x, const float y, const float width, const float height, const int windowWidth, const int windowHeight);
~Box();
void render();
void resize(int width, int height);
void pointToViewport(float &x, float &y, int windowWidth, int windowHeight);
void distanceToViewport(float &x, float &y, int windowWidth, int windowHeight);
void resize(const int width, const int height);
void pointToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const ;
void distanceToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const ;
};
#endif

36
src/graphics/opengl/Text.cpp

@ -2,40 +2,44 @@ @@ -2,40 +2,44 @@
#include "../text/TextRasterizer.h"
#include <memory>
Text::Text(std::string text, int x, int y, int windowWidth, int windowHeight) {
Text::Text(const std::string &text, const int x, const int y, const int windowWidth, const int windowHeight) {
this->x = x;
this->y = y;
std::unique_ptr<TextRasterizer> textRasterizer = std::unique_ptr<TextRasterizer>(new TextRasterizer("DejaVuSerif.ttf", 50, 72));
const std::unique_ptr<TextRasterizer> textRasterizer = std::unique_ptr<TextRasterizer>(new TextRasterizer("DejaVuSerif.ttf", 50, 72));
unsigned int glyphCount;
std::unique_ptr<Glyph[]> glyphs = textRasterizer->rasterize(text, x, y, glyphCount);
std::unique_ptr<const Glyph[]> glyphs = textRasterizer->rasterize(text, x, y, glyphCount);
if (glyphs == nullptr) {
return;
}
for (int i = 0; i < glyphCount; i++) {
Glyph &glyph = glyphs[i];
const Glyph &glyph = glyphs[i];
pointToViewport(glyph.x0, glyph.y0, windowWidth, windowHeight);
pointToViewport(glyph.x1, glyph.y1, windowWidth, windowHeight);
float vx0 = glyph.x0;
float vy0 = glyph.y0;
float vx1 = glyph.x1;
float vy1 = glyph.y1;
pointToViewport(vx0, vy0, windowWidth, windowHeight);
pointToViewport(vx1, vy1, windowWidth, windowHeight);
float *vertices = new float[20];
vertices[(0 * 5) + 0] = glyph.x0;
vertices[(0 * 5) + 1] = glyph.y0;
vertices[(0 * 5) + 0] = vx0;
vertices[(0 * 5) + 1] = vy0;
vertices[(0 * 5) + 2] = 0.0f;
vertices[(0 * 5) + 3] = glyph.s0;
vertices[(0 * 5) + 4] = glyph.t0;
vertices[(1 * 5) + 0] = glyph.x0;
vertices[(1 * 5) + 1] = glyph.y1;
vertices[(1 * 5) + 0] = vx0;
vertices[(1 * 5) + 1] = vy1;
vertices[(1 * 5) + 2] = 0.0f;
vertices[(1 * 5) + 3] = glyph.s0;
vertices[(1 * 5) + 4] = glyph.t1;
vertices[(2 * 5) + 0] = glyph.x1;
vertices[(2 * 5) + 1] = glyph.y1;
vertices[(2 * 5) + 0] = vx1;
vertices[(2 * 5) + 1] = vy1;
vertices[(2 * 5) + 2] = 0.0f;
vertices[(2 * 5) + 3] = glyph.s1;
vertices[(2 * 5) + 4] = glyph.t1;
vertices[(3 * 5) + 0] = glyph.x1;
vertices[(3 * 5) + 1] = glyph.y0;
vertices[(3 * 5) + 0] = vx1;
vertices[(3 * 5) + 1] = vy0;
vertices[(3 * 5) + 2] = 0.0f;
vertices[(3 * 5) + 3] = glyph.s1;
vertices[(3 * 5) + 4] = glyph.t0;
@ -95,7 +99,7 @@ void Text::render() { @@ -95,7 +99,7 @@ void Text::render() {
}
}
void Text::resize(float sx, float sy) {
void Text::resize(const float sx, const float sy) {
for (int i = 0; i < glyphVertices.size(); i++) {
glyphVertices[i][(0 * 5) + 0] = ((glyphVertices[i][(0 * 5) + 0] + 1) / sx) - 1;
glyphVertices[i][(0 * 5) + 1] = ((glyphVertices[i][(0 * 5) + 1] + 1) / sy) - 1;
@ -109,7 +113,7 @@ void Text::resize(float sx, float sy) { @@ -109,7 +113,7 @@ void Text::resize(float sx, float sy) {
verticesDirty = true;
}
void Text::pointToViewport(float &x, float &y, int windowWidth, int windowHeight) {
void Text::pointToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const {
x = ((x / windowWidth) * 2) - 1;
y = ((y / windowHeight) * 2) - 1;
}

8
src/graphics/opengl/Text.h

@ -12,7 +12,7 @@ private: @@ -12,7 +12,7 @@ private:
float width;
float height;
bool verticesDirty = false;
unsigned int indices[6] = {
const unsigned int indices[6] = {
0, 1, 2,
0, 2, 3
};
@ -23,11 +23,11 @@ private: @@ -23,11 +23,11 @@ private:
std::vector<GLuint> elementBufferObjects;
std::vector<GLuint> textures;
public:
Text(std::string text, int x, int y, int windowWidth, int windowHeight);
Text(const std::string &text, const int x, const int y, const int windowWidth, const int windowHeight);
~Text();
void render();
void resize(float sx, float sy);
void pointToViewport(float &x, float &y, int windowWidth, int windowHeight);
void resize(const float sx, const float sy);
void pointToViewport(float &x, float &y, const int windowWidth, const int windowHeight) const;
};
#endif

34
src/graphics/opengl/Window.cpp

@ -48,14 +48,14 @@ bool Window::initGLFW() { @@ -48,14 +48,14 @@ bool Window::initGLFW() {
glfwSetFramebufferSizeCallback(window, [](GLFWwindow *window, int width, int height) {
glViewport(0, 0, width, height);
Window *thiz = reinterpret_cast<Window*>(glfwGetWindowUserPointer(window));
float sx = (float) width / thiz->windowWidth;
float sy = (float) height / thiz->windowHeight;
const float sx = (float) width / thiz->windowWidth;
const float sy = (float) height / thiz->windowHeight;
thiz->windowWidth = width;
thiz->windowHeight = height;
for (std::unique_ptr<Box> &box : thiz->boxes) {
for (const std::unique_ptr<Box> &box : thiz->boxes) {
box->resize(width, height);
}
for (std::unique_ptr<Text> &text : thiz->texts) {
for (const std::unique_ptr<Text> &text : thiz->texts) {
text->resize(sx, sy);
}
});
@ -64,9 +64,9 @@ bool Window::initGLFW() { @@ -64,9 +64,9 @@ bool Window::initGLFW() {
return true;
}
bool Window::initGLEW() {
bool Window::initGLEW() const {
glewExperimental = GL_TRUE;
GLenum err = glewInit();
const GLenum err = glewInit();
if (err != GLEW_OK) {
std::cout << "Could not initialize GLEW: " << glewGetErrorString(err) << std::endl;
return false;
@ -88,21 +88,21 @@ bool Window::initGL() { @@ -88,21 +88,21 @@ bool Window::initGL() {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
GLuint fontVertexShader = compileShader(GL_VERTEX_SHADER, fontVertexShaderSource);
GLuint fontFragmentShader = compileShader(GL_FRAGMENT_SHADER, fontFragmentShaderSource);
const GLuint fontVertexShader = compileShader(GL_VERTEX_SHADER, fontVertexShaderSource);
const GLuint fontFragmentShader = compileShader(GL_FRAGMENT_SHADER, fontFragmentShaderSource);
fontProgram = compileProgram(fontVertexShader, fontFragmentShader);
glDeleteShader(fontVertexShader);
glDeleteShader(fontFragmentShader);
GLuint textureVertexShader = compileShader(GL_VERTEX_SHADER, textureVertexShaderSource);
GLuint textureFragmentShader = compileShader(GL_FRAGMENT_SHADER, textureFragmentShaderSource);
const GLuint textureVertexShader = compileShader(GL_VERTEX_SHADER, textureVertexShaderSource);
const GLuint textureFragmentShader = compileShader(GL_FRAGMENT_SHADER, textureFragmentShaderSource);
textureProgram = compileProgram(textureVertexShader, textureFragmentShader);
glDeleteShader(textureVertexShader);
glDeleteShader(textureFragmentShader);
}
GLuint Window::compileShader(GLenum shaderType, const char *shaderSource) {
GLuint shader = glCreateShader(shaderType);
const GLuint Window::compileShader(const GLenum shaderType, const char *shaderSource) const {
const GLuint shader = glCreateShader(shaderType);
glShaderSource(shader, 1, &shaderSource, NULL);
glCompileShader(shader);
@ -117,8 +117,8 @@ GLuint Window::compileShader(GLenum shaderType, const char *shaderSource) { @@ -117,8 +117,8 @@ GLuint Window::compileShader(GLenum shaderType, const char *shaderSource) {
return shader;
}
GLuint Window::compileProgram(GLuint vertexShader, GLuint fragmentShader) {
GLuint program = glCreateProgram();
const GLuint Window::compileProgram(const GLuint vertexShader, const GLuint fragmentShader) const {
const GLuint program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glLinkProgram(program);
@ -134,14 +134,14 @@ GLuint Window::compileProgram(GLuint vertexShader, GLuint fragmentShader) { @@ -134,14 +134,14 @@ GLuint Window::compileProgram(GLuint vertexShader, GLuint fragmentShader) {
return program;
}
void Window::render() {
void Window::render() const {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(textureProgram);
for (std::unique_ptr<Box> &box : boxes) {
for (const std::unique_ptr<Box> &box : boxes) {
box->render();
}
glUseProgram(fontProgram);
for (std::unique_ptr<Text> &text : texts) {
for (const std::unique_ptr<Text> &text : texts) {
text->render();
}
glfwPollEvents();

8
src/graphics/opengl/Window.h

@ -16,11 +16,11 @@ public: @@ -16,11 +16,11 @@ public:
~Window();
bool init();
bool initGLFW();
bool initGLEW();
bool initGLEW() const;
bool initGL();
GLuint compileShader(GLenum shaderType, const char *shaderSource);
GLuint compileProgram(GLuint vertexShader, GLuint fragmentShader);
void render();
const GLuint compileShader(const GLenum shaderType, const char *shaderSource) const;
const GLuint compileProgram(const GLuint vertexShader, const GLuint fragmentShader) const;
void render() const;
GLFWwindow *window;
int windowWidth;
int windowHeight;

37
src/graphics/text/TextRasterizer.cpp

@ -3,7 +3,7 @@ @@ -3,7 +3,7 @@
#include <limits>
#include <iostream>
TextRasterizer::TextRasterizer(std::string fontPath, int size, int resolution) {
TextRasterizer::TextRasterizer(const std::string &fontPath, int size, int resolution) {
FT_Init_FreeType(&lib);
face = std::make_unique<FT_Face>();
@ -35,35 +35,38 @@ TextRasterizer::~TextRasterizer() { @@ -35,35 +35,38 @@ TextRasterizer::~TextRasterizer() {
FT_Done_FreeType(lib);
}
std::unique_ptr<Glyph[]> TextRasterizer::rasterize(std::string text, int x, int y, unsigned int &glyphCount) {
std::unique_ptr<const Glyph[]> TextRasterizer::rasterize(const std::string &text, const int x, const int y, unsigned int &glyphCount) const {
hb_buffer_reset(buffer);
hb_buffer_set_direction(buffer, HB_DIRECTION_LTR);
hb_buffer_set_language(buffer, hb_language_from_string("en", 2));
hb_buffer_add_utf8(buffer, "Hello, World!", text.length(), 0, text.length());
hb_buffer_add_utf8(buffer, text.c_str(), text.length(), 0, text.length());
hb_feature_t KerningOn = { HB_TAG('k', 'e', 'r', 'n'), 1, 0, std::numeric_limits<unsigned int>::max() };
const hb_feature_t KerningOn = { HB_TAG('k', 'e', 'r', 'n'), 1, 0, std::numeric_limits<unsigned int>::max() };
hb_shape(font, buffer, &KerningOn, 1);
hb_glyph_info_t *glyphInfo = hb_buffer_get_glyph_infos(buffer, &glyphCount);
hb_glyph_position_t *glyphPos = hb_buffer_get_glyph_positions(buffer, &glyphCount);
const hb_glyph_info_t *glyphInfo = hb_buffer_get_glyph_infos(buffer, &glyphCount);
const hb_glyph_position_t *glyphPos = hb_buffer_get_glyph_positions(buffer, &glyphCount);
std::unique_ptr<Glyph[]> glyphs = std::unique_ptr<Glyph[]>(new Glyph[glyphCount]);
int cx = x;
int cy = y;
for (int i = 0; i < glyphCount; i++) {
if (FT_Load_Glyph(*face, glyphInfo[i].codepoint, FT_LOAD_DEFAULT)) {
std::cout << "Could not load glyph" << std::endl;
return nullptr;
}
FT_GlyphSlot slot = (*face)->glyph;
const FT_GlyphSlot slot = (*face)->glyph;
if (FT_Render_Glyph(slot, FT_RENDER_MODE_NORMAL)) {
std::cout << "Could not render glyph" << std::endl;
return nullptr;
}
FT_Bitmap ftBitmap = slot->bitmap;
const FT_Bitmap ftBitmap = slot->bitmap;
glyphs[i].textureWidth = pow(2, ceil(log(ftBitmap.width) / log(2)));
glyphs[i].textureHeight = pow(2, ceil(log(ftBitmap.rows) / log(2)));
@ -72,13 +75,13 @@ std::unique_ptr<Glyph[]> TextRasterizer::rasterize(std::string text, int x, int @@ -72,13 +75,13 @@ std::unique_ptr<Glyph[]> TextRasterizer::rasterize(std::string text, int x, int
memcpy(glyphs[i].textureData.get() + iy * glyphs[i].textureWidth, ftBitmap.buffer + iy * ftBitmap.width, ftBitmap.width);
}
float xa = (float) glyphPos[i].x_advance / 64;
float ya = (float) glyphPos[i].y_advance / 64;
float xo = (float) glyphPos[i].x_offset / 64;
float yo = (float) glyphPos[i].y_offset / 64;
const float xa = (float) glyphPos[i].x_advance / 64;
const float ya = (float) glyphPos[i].y_advance / 64;
const float xo = (float) glyphPos[i].x_offset / 64;
const float yo = (float) glyphPos[i].y_offset / 64;
glyphs[i].x0 = x + xo + slot->bitmap_left;
glyphs[i].y0 = floor(y + yo + slot->bitmap_top);
glyphs[i].x0 = cx + xo + slot->bitmap_left;
glyphs[i].y0 = floor(cy + yo + slot->bitmap_top);
glyphs[i].x1 = glyphs[i].x0 + ftBitmap.width;
glyphs[i].y1 = floor(glyphs[i].y0 - ftBitmap.rows);
@ -87,14 +90,14 @@ std::unique_ptr<Glyph[]> TextRasterizer::rasterize(std::string text, int x, int @@ -87,14 +90,14 @@ std::unique_ptr<Glyph[]> TextRasterizer::rasterize(std::string text, int x, int
glyphs[i].s1 = (float) ftBitmap.width / glyphs[i].textureWidth;
glyphs[i].t1 = (float) ftBitmap.rows / glyphs[i].textureHeight;
x += xa;
y += ya;
cx += xa;
cx += ya;
}
return glyphs;
}
bool TextRasterizer::isUnicodeBMP(FT_Face face) {
const bool TextRasterizer::isUnicodeBMP(const FT_Face &face) const {
for (int i = 0; i < face->num_charmaps; i++) {
if (((face->charmaps[i]->platform_id == 0) && (face->charmaps[i]->encoding_id == 3)) || ((face->charmaps[i]->platform_id == 3) && (face->charmaps[i]->encoding_id == 1))) {
FT_Set_Charmap(face, face->charmaps[i]);

6
src/graphics/text/TextRasterizer.h

@ -22,10 +22,10 @@ struct Glyph { @@ -22,10 +22,10 @@ struct Glyph {
class TextRasterizer {
public:
TextRasterizer(std::string font, int size, int resolution);
TextRasterizer(const std::string &fontPath, int size, int resolution);
~TextRasterizer();
std::unique_ptr<Glyph[]> rasterize(std::string text, int x, int y, unsigned int &glyphCount);
bool isUnicodeBMP(FT_Face face);
std::unique_ptr<const Glyph[]> rasterize(const std::string &text, const int x, const int y, unsigned int &glyphCount) const;
const bool isUnicodeBMP(const FT_Face &face) const;
FT_Library lib;
hb_font_t *font;
hb_buffer_t *buffer;

10
src/html/HTMLParser.cpp

@ -12,7 +12,7 @@ void printNode(Node *node, int indent) { @@ -12,7 +12,7 @@ void printNode(Node *node, int indent) {
}
if (node->nodeType == NodeType::TAG) {
std::cout << "TAG: " << dynamic_cast<TagNode*>(node)->tag << std::endl;
for (std::pair<std::string, std::string> property : dynamic_cast<TagNode*>(node)->properties) {
for (const std::pair<std::string, std::string> property : dynamic_cast<TagNode*>(node)->properties) {
for (int i = 0; i < indent; i++) {
std::cout << '\t';
}
@ -28,7 +28,7 @@ void printNode(Node *node, int indent) { @@ -28,7 +28,7 @@ void printNode(Node *node, int indent) {
}
}
void HTMLParser::parse(std::string html) {
void HTMLParser::parse(const std::string &html) const {
Node rootNode = Node(NodeType::ROOT);
Node *currentNode = &rootNode;
std::vector<int> starts;
@ -73,7 +73,7 @@ void HTMLParser::parse(std::string html) { @@ -73,7 +73,7 @@ void HTMLParser::parse(std::string html) {
}
else if (state == 2) { // Tag
if (html[cursor] == '>') {
int start = starts.back();
const int start = starts.back();
starts.pop_back();
std::string element = html.substr(start, cursor - start + 1);
parseTag(element, dynamic_cast<TagNode*>(currentNode));
@ -82,7 +82,7 @@ void HTMLParser::parse(std::string html) { @@ -82,7 +82,7 @@ void HTMLParser::parse(std::string html) {
}
else if (state == 3) { // Text
if (html[cursor + 1] == '<' && html[cursor + 2] == '/') {
int start = starts.back();
const int start = starts.back();
starts.pop_back();
dynamic_cast<TextNode*>(currentNode)->text = html.substr(start, cursor - start + 1);
state = 0;
@ -93,7 +93,7 @@ void HTMLParser::parse(std::string html) { @@ -93,7 +93,7 @@ void HTMLParser::parse(std::string html) {
printNode(&rootNode, 0);
}
void HTMLParser::parseTag(std::string element, TagNode* tagNode) {
void HTMLParser::parseTag(const std::string &element, TagNode* tagNode) const {
int cursor;
int start = 1; // skip first <
int state = 0;

4
src/html/HTMLParser.h

@ -7,8 +7,8 @@ @@ -7,8 +7,8 @@
class HTMLParser {
public:
void parse(std::string html);
void parseTag(std::string element, TagNode* tagNode);
void parse(const std::string &html) const;
void parseTag(const std::string &element, TagNode* tagNode) const;
};
#endif

16
src/main.cpp

@ -7,7 +7,7 @@ @@ -7,7 +7,7 @@
#include <iostream>
#include <memory>
std::string getDocumentFromURL(std::string url) {
const std::string getDocumentFromURL(const std::string &url) {
int slashes = 0;
for (int i = 0; i < url.length(); i++) {
if (url[i] == '/') {
@ -19,7 +19,7 @@ std::string getDocumentFromURL(std::string url) { @@ -19,7 +19,7 @@ std::string getDocumentFromURL(std::string url) {
}
}
std::string getHostFromURL(std::string url) {
const std::string getHostFromURL(const std::string &url) {
int slashes = 0;
int start = 0;
for (int i = 0; i < url.length(); i++) {
@ -33,16 +33,16 @@ std::string getHostFromURL(std::string url) { @@ -33,16 +33,16 @@ std::string getHostFromURL(std::string url) {
}
}
void handleRequest(HTTPResponse response) {
void handleRequest(const HTTPResponse &response) {
if (response.statusCode == 200) {
std::unique_ptr<HTMLParser> parser = std::unique_ptr<HTMLParser>(new HTMLParser());
const std::unique_ptr<HTMLParser> parser = std::unique_ptr<HTMLParser>(new HTMLParser());
parser->parse(response.body);
}
else if (response.statusCode == 301) {
std::string location = response.properties["Location"];
const std::string location = response.properties.at("Location");
std::cout << "Redirect To: " << location << std::endl;
std::cout << getHostFromURL(location) << "!" << getDocumentFromURL(location) << std::endl;
std::unique_ptr<HTTPRequest> request = std::unique_ptr<HTTPRequest>(new HTTPRequest(getHostFromURL(location), getDocumentFromURL(location)));
const std::unique_ptr<HTTPRequest> request = std::unique_ptr<HTTPRequest>(new HTTPRequest(getHostFromURL(location), getDocumentFromURL(location)));
request->sendRequest(handleRequest);
return;
}
@ -56,9 +56,9 @@ int main(int argc, char *argv[]) { @@ -56,9 +56,9 @@ int main(int argc, char *argv[]) {
std::cout << "./netrunner <url>" << std::endl;
return 1;
}
std::unique_ptr<HTTPRequest> request = std::unique_ptr<HTTPRequest>(new HTTPRequest(getHostFromURL(argv[1]), getDocumentFromURL(argv[1])));
const std::unique_ptr<HTTPRequest> request = std::unique_ptr<HTTPRequest>(new HTTPRequest(getHostFromURL(argv[1]), getDocumentFromURL(argv[1])));
request->sendRequest(handleRequest);
std::unique_ptr<Window> window = std::make_unique<Window>();
const std::unique_ptr<Window> window = std::make_unique<Window>();
window->init();
while (!glfwWindowShouldClose(window->window)) {
window->render();

16
src/networking/HTTPRequest.cpp

@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
#include <sys/types.h>
#include <sys/socket.h>
HTTPRequest::HTTPRequest(std::string host, std::string document) {
HTTPRequest::HTTPRequest(const std::string &host, const std::string &document) {
this->document = document;
this->version = Version::HTTP10;
this->method = Method::GET;
@ -14,20 +14,20 @@ HTTPRequest::HTTPRequest(std::string host, std::string document) { @@ -14,20 +14,20 @@ HTTPRequest::HTTPRequest(std::string host, std::string document) {
this->userAgent = "NetRunner";
}
bool HTTPRequest::sendRequest(std::function<void(HTTPResponse)> responseCallback) {
const bool HTTPRequest::sendRequest(std::function<void(const HTTPResponse&)> responseCallback) const {
struct addrinfo hints;
struct addrinfo *servinfo;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
int res = getaddrinfo(host.c_str(), "80", &hints, &servinfo);
const int res = getaddrinfo(host.c_str(), "80", &hints, &servinfo);
if (res != 0) {
std::cout << "Could not lookup " << host << ": " << res << std::endl;
return false;
}
int sock = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
const int sock = socket(servinfo->ai_family, servinfo->ai_socktype, servinfo->ai_protocol);
if (sock == -1) {
std::cout << "Could not create socket: " << errno << std::endl;
freeaddrinfo(servinfo);
@ -40,8 +40,8 @@ bool HTTPRequest::sendRequest(std::function<void(HTTPResponse)> responseCallback @@ -40,8 +40,8 @@ bool HTTPRequest::sendRequest(std::function<void(HTTPResponse)> responseCallback
return false;
}
std::string request = methodToString(method) + std::string(" ") + document + std::string(" ") + versionToString(version) + std::string("\r\nHost: ") + host + std::string("\r\nUser-Agent: ") + userAgent + std::string("\r\n\r\n");
size_t sent = send(sock, request.c_str(), request.length(), 0);
const std::string request = methodToString(method) + std::string(" ") + document + std::string(" ") + versionToString(version) + std::string("\r\nHost: ") + host + std::string("\r\nUser-Agent: ") + userAgent + std::string("\r\n\r\n");
const size_t sent = send(sock, request.c_str(), request.length(), 0);
if (sent == -1) {
std::cout << "Could not send \"" << request << "\": " << errno << std::endl;
freeaddrinfo(servinfo);
@ -61,7 +61,7 @@ bool HTTPRequest::sendRequest(std::function<void(HTTPResponse)> responseCallback @@ -61,7 +61,7 @@ bool HTTPRequest::sendRequest(std::function<void(HTTPResponse)> responseCallback
return true;
}
std::string HTTPRequest::versionToString(Version version) {
const std::string HTTPRequest::versionToString(const Version version) const {
switch (version) {
case Version::HTTP10:
return "HTTP/1.0";
@ -69,7 +69,7 @@ std::string HTTPRequest::versionToString(Version version) { @@ -69,7 +69,7 @@ std::string HTTPRequest::versionToString(Version version) {
return "ERROR";
}
std::string HTTPRequest::methodToString(Method method) {
const std::string HTTPRequest::methodToString(const Method method) const {
switch (method) {
case Method::GET:
return "GET";

8
src/networking/HTTPRequest.h

@ -16,10 +16,10 @@ enum class Method { @@ -16,10 +16,10 @@ enum class Method {
class HTTPRequest {
public:
HTTPRequest(std::string host, std::string document);
bool sendRequest(std::function<void(HTTPResponse)> responseCallback);
std::string versionToString(Version version);
std::string methodToString(Method method);
HTTPRequest(const std::string &host, const std::string &document);
const bool sendRequest(std::function<void(const HTTPResponse&)> responseCallback) const;
const std::string versionToString(const Version version) const;
const std::string methodToString(const Method method) const;
private:
std::string document;
Version version;

2
src/networking/HTTPResponse.cpp

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
#include "HTTPResponse.h"
HTTPResponse::HTTPResponse(std::string rawResponse) {
HTTPResponse::HTTPResponse(const std::string &rawResponse) {
int state = 0;
int cursor = 0;
int start = 0;

2
src/networking/HTTPResponse.h

@ -6,7 +6,7 @@ @@ -6,7 +6,7 @@
class HTTPResponse {
public:
HTTPResponse(std::string rawRequest);
HTTPResponse(const std::string &rawRequest);
std::string version;
std::string status;
std::map<std::string, std::string> properties;

Loading…
Cancel
Save