PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PEBLHTTP.h
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: utility/PEBLHTTP.h
4// Purpose: Class enabling HTTP GET and POST commands.
5// Author: Shane T. Mueller, Ph.D.
6// Copyright: (c) 2013-2026 Shane T. Mueller <smueller@obereed.net>
7// License: GPL 2
8//
9//
10//
11// This file is part of the PEBL project.
12//
13// PEBL is free software; you can redistribute it and/or modify
14// it under the terms of the GNU General Public License as published by
15// the Free Software Foundation; either version 2 of the License, or
16// (at your option) any later version.
17//
18// PEBL is distributed in the hope that it will be useful,
19// but WITHOUT ANY WARRANTY; without even the implied warranty of
20// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21// GNU General Public License for more details.
22//
23// You should have received a copy of the GNU General Public License
24// along with PEBL; if not, write to the Free Software
25// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27#ifndef __PEBLHTTP_H__
28#define __PEBLHTTP_H__
29
30
31#define PEBL_HAPPY 1
32#define PEBL_CURL 2
33#define PEBL_FETCH 3
34
35// HTTP_LIB is defined in the Makefile via compiler flags:
36// -DHTTP_LIB=2 (PEBL_CURL) for Linux native builds
37// -DHTTP_LIB=3 (PEBL_FETCH) for Emscripten builds
38// Do not redefine it here to avoid macro redefinition warnings
39
40
41#ifdef PEBL_HTTP
42
43#if HTTP_LIB == PEBL_HAPPY
44#include "happyhttp.h"
45#elif HTTP_LIB == PEBL_CURL
46#include <curl/curl.h>
47#endif
48
49#include "../base/Variant.h"
50#include <iostream>
51#include <stdlib.h>
52#include <cstdio>
53#include <cstring>
54
55
56// This uses a simple http library called 'happyhttp'
57// it is a slim wrapper around that, but we abstract from.
58// aLSO , this should enable using PEBLPaths, PErrors, and such.
59// the library to maybe someday use curl or the wget commands
60// in emscripten.
61
62class PEBLHTTP
63{
64
65public:
66 PEBLHTTP(Variant host,int port=80); //with no port, default to 80
67 virtual ~PEBLHTTP();
68
69 virtual int GetHTTPFile(Variant fname, Variant savename);
70 virtual std::string GetHTTPText(Variant fname);
71
72 // virtual int PostHTTPold(Variant name, Variant headers,Variant body); //original; for posterity.
73 virtual Variant PostHTTP(Variant name, Variant args,
74 Variant body);
75 virtual Variant PostMulti(Variant pagename, Variant args,
76 Variant uploadname,Variant name);
77
78 virtual FILE * GetFileObject(){return mFile;};
79 virtual std::string * GetTextObject(){return &mText;};
80
81 virtual void SetByteCount(int n){mByteCount = n;}
82 virtual int GetByteCount(){return mByteCount;};
83
84 virtual void SetStatus(int status){mStatus= status;};
85 virtual int GetStatus(){return mStatus;};
86
87 virtual void SetReason(std::string reason){mReason = reason;};
88
89
90 FILE* mFile; //file to stream to.
91
92
93
94 private:
95 int mStatus; //http status number
96 std::string mReason;
97
98 std::string mText;
99 Variant mHost;
100 int mPort;
101
102
103#if HTTP_LIB == PEBL_CURL
104 CURL * mCurl;
105#endif
106
107
108 Variant mFileName;
109 Variant mSaveName;
110
111
112
113 int mByteCount;
114};
115#endif
116#endif