141
Ohjelmointi, palvelimet ja muu edistyneempi käyttö / Vs: C-kieli
« : 02.04.22 - klo:21.15 »Tämän säikeen ansiosta kokeilin ensimmäisen kerran
Koodia: [Valitse]
echo $PATH
ja, tosiaan, vaikuttaa siltä, että sillä saa polun näkyviin
Ubuntu 24.04 LTS | Lataa ja asenna | Tutustu yhteisöön | Blogi | Yritysten tarjoamat palvelut
Liity Ubuntu Suomen seuraan: Facebook tai Twitter
Tässä osiossa voit tarkastella kaikkia tämän jäsenen viestejä. Huomaa, että näet viestit vain niiltä alueilta, joihin sinulla on pääsy.
echo $PATH
NOTICE: Not enabling PHP 7.2 FPM by default.
NOTICE: To enable PHP 7.2 FPM in Apache2 do:
NOTICE: a2enmod proxy_fcgi setenvif
NOTICE: a2enconf php7.2-fpm
NOTICE: You are seeing this message because you have apache2 package installed.
Setting up php7.2 (7.2.24-0ubuntu0.18.04.11) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for ureadahead (0.100.0-21) ...
Processing triggers for systemd (237-3ubuntu10.53) ...
$ php -v
PHP 7.2.24-0ubuntu0.18.04.11 (cli)
class file_response_resource3 : public httpserver::http_resource
{
public:
const std::shared_ptr<httpserver::http_response> render_POST(const httpserver::http_request& req)
{
std::string datapar06 = req.get_arg("data06"); // ??????
std::cout << "!!!!!!!data06 on nyt:" << datapar06 << std::endl; // ??????
return std::shared_ptr<httpserver::file_response>(new httpserver::file_response("textareafile.html", 200, "text/html"));
}
}; // luokka file_response_resource3 loppuu
async function fetch_textareafile02() // postina, libhttpserverissä
{ // laitettava postiksi
let response = await fetch('textareafile.html',
{
method: 'POST',
headers: {
"Content-Type": "text/plain;charset=UTF-8"
},
body: { 'data06': 'ÖÅÖÅÖÅÖÅÖÅÖÅÖÅÖÅÖÅÖÅ ' }
}
);
let text = await response.text();
form_var = document.getElementById("form01");
textarea_var = form_var.tekstialue_id;
textarea_var.value += text + '\t';
}
http://localhost:8080/testisy%C3%B6tt%C3%B601.html
#include <iostream>
#include "/home/k1/Documents/my_libhttpserver/libhttpserveri/libhttpserver/src/httpserver.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <bits/stdc++.h>
std::string filetostring(const std::string& filename)
{
std::fstream newfile;
std::string string1; // tiedosto stringinä
newfile.open(filename, std::ios::in);
if(newfile.is_open() )
{
std::string row1;
while(getline(newfile, row1))
{
string1 += '\n' + row1;
} // while loppuu
} // if loppuu
return string1;
} // filetostring loppuu
// resurssi vastaa esimerkiksi html-sivua asiakkaan puolella
// luokan hello_world_resource määrittely
class hello_world_resource : public httpserver::http_resource
{
public:
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&);
}; // luokka hello_world_resource loppuu
// Using the render method you are able to catch each type of request you receive
// resurssilla on oma renderöijä, luokan hello_world_resource renderöijä alla
const std::shared_ptr<httpserver::http_response> hello_world_resource::render(const httpserver::http_request& req) // pyynnön parametrien nimet rekisteröidään
{
std::cout << std::endl;
std::string datapar01 = req.get_arg("data01");
std::string datapar02 = req.get_arg("data02");
std::string datapar03 = req.get_arg("data03");
std::string datapar04 = req.get_arg("data04");
std::string datapar05 = req.get_arg("data05");
std::string datapar06 = req.get_arg("data06");
std::string datapar07 = req.get_arg("data07");
std::string datapar08 = req.get_arg("data08");
std::string datapar09 = req.get_arg("data09");
std::string datapar10 = req.get_arg("data10");
std::string datapar11 = req.get_arg("data11");
// aputulostuksia rekiströityjen parametriniemien syötearvoista
std::cout << "Now data 1 is:" << datapar01 << std::endl;
std::cout << "Now data 2 is:" << datapar02 << std::endl;
std::cout << "Now data 3 is:" << datapar03 << std::endl;
// response on asiakkaalle renderöijän asiakkaalle toimittama sivu
return std::shared_ptr<httpserver::http_response>(new
httpserver::string_response("viesti palvelimelta", 200, "text/plain"));
//tai httpserver::string_response("viesti palvelimelta", 200, "text/html"));
} // resurssin hello_world_resource renderöijän määrittely loppuu tähän
// luokan file_response_resource määrittely
class file_response_resource : public httpserver::http_resource
{
public:
const std::shared_ptr<httpserver::http_response> render_GET(const httpserver::http_request&)
{
return std::shared_ptr<httpserver::file_response>(new httpserver::file_response("testisyöttö01.html", 200, "text/html"));
}
}; // luokka file_response_resource loppuu
int main()
{ // porttinumero ja säiemäärä parametreina
httpserver::webserver ws = httpserver::create_webserver(8080).start_method(httpserver::http::http_utils::INTERNAL_SELECT).max_threads(5);
hello_world_resource hwr;
file_response_resource frr;
ws.register_resource("/hello", &hwr, true); // tällä kutsutaan
ws.register_resource("/testisyöttö01.html", &frr, true); // tällä kutsutaan
ws.start(true); // blocking on true, non blocking olisi false
return 0;
}
g++ libhttpserver03.cpp -o libhttpserver03 -Wall -pedantic -L/usr/local/lib -lhttpserver -lmicrohttpd -pthread -std=c++17
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at file:///hello. (Reason: CORS request not http).
Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource.
test_fun file:///home/k1/Documents/my_libhttpserver/testejä/testisyöttö01.html:25
onclick file:///home/k1/Documents/my_libhttpserver/testejä/testisyöttö01.html:1
#include <iostream>
#include "/home/k1/Documents/my_libhttpserver/libhttpserveri/libhttpserver/src/httpserver.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <bits/stdc++.h>
std::string filetostring(const std::string& filename)
{
std::fstream newfile;
std::string string1; // tiedosto stringinä
newfile.open(filename, std::ios::in);
if(newfile.is_open() )
{
std::string row1;
while(getline(newfile, row1))
{
string1 += '\n' + row1;
} // while loppuu
} // if loppuu
return string1;
} // filetostring loppuu
class hello_world_resource : public httpserver::http_resource
{
public:
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&);
std::string data01, data02;
};
// Using the render method you are able to catch each type of request you receive
const std::shared_ptr<httpserver::http_response> hello_world_resource::render(const httpserver::http_request& req)
{
std::cout << std::endl;
std::string datapar01 = req.get_arg("data01");
std::string datapar02 = req.get_arg("data02");
std::string datapar03 = req.get_arg("data03");
std::string datapar04 = req.get_arg("data04");
std::string datapar05 = req.get_arg("data05");
std::string datapar06 = req.get_arg("data06");
std::string datapar07 = req.get_arg("data07");
std::string datapar08 = req.get_arg("data08");
std::string datapar09 = req.get_arg("data09");
std::string datapar10 = req.get_arg("data10");
std::string datapar11 = req.get_arg("data11");
std::cout << "Now data 1 is:" << datapar01 << std::endl;
std::cout << "Now data 2 is:" << datapar02 << std::endl;
std::cout << "Now data 3 is:" << datapar03 << std::endl;
return std::shared_ptr<httpserver::http_response>(new
//httpserver::string_response("viesti palvelimelta", 200, "text/html"));
httpserver::string_response("viesti palvelimelta", 200, "text/plain"));
}
int main()
{ // porttinumero ja säiemäärä parametreina
httpserver::webserver ws = httpserver::create_webserver(8080).start_method(httpserver::http::http_utils::INTERNAL_SELECT).max_threads(5);
hello_world_resource hwr;
ws.register_resource("/hello", &hwr, true); // tällä kutsutaan
ws.start(true); // blocking on true, non blocking olisi false
return 0;
}
// g++ libhttpserver03.cpp -o libhttpserver03 -Wall -pedantic -L/usr/local/lib -lhttpserver -lmicrohttpd -pthread -std=c++17
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title> testisyöttö, versio 01 </title>
</head>
<body style="text-align: left;">
<h1 style="color: green;"> Testisyöttö 01 </h1>
<p> Napsauta kaavake auki </p>
<button onClick="test_fun()">Avaa kaavake </button>
<p id="prgh"></p>
<script>
async function test_fun()
{
alert("kokeilufunktio alkaa");
let response = await fetch('/hello');
let text = await response.text(); // read response body as text
alert(text.slice(0, 80) + '...');
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title> syöttökaavake, versio 01 </title>
</head>
<body style="text-align: left;">
<h1 style="color: green;"> Syöttökaavake </h1>
<p> Napsauta kaavake auki </p>
<button onClick="fetch_test_fun()">Avaa kaavake </button>
<p id="prgh"></p>
<script>
function fetch_test_fun()
{
alert("kokeilufunktio alkaa");
let response = await fetch("/hello");
//let text = await response.text(); // read response body as text
//alert(text.slice(0, 80) + '...');
}
</script>
</body>
</html>
<!--
https://javascript.info/fetch
https://javascript.info/xmlhttprequest
-->
HTML-sivu olisi parempi tallentaa erilliseen tiedostoon ja lukea sieltä vaikka ajon aikana.
Jos tarkoituksena on rakentaa jonkinlainen templaattimekanismi joka täydentää ja tarjoaa www-sivuja web-palvelimen tapaan, suosittelisin tutkimaan myös valmiita ratkaisuja. Pyörää ei yleensä kannata keksiä uudelleen ainakaan alusta lähtien.
#include <iostream>
#include "/home/k1/Documents/my_libhttpserver/libhttpserveri/libhttpserver/src/httpserver.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <bits/stdc++.h>
std::string filetostring(const std::string& filename)
{
std::fstream newfile;
std::string string1; // tiedosto stringinä
newfile.open(filename, std::ios::in);
if(newfile.is_open() )
{
std::string row1;
while(getline(newfile, row1))
{
string1 += '\n' + row1;
} // while loppuu
} // if loppuu
return string1;
} // filetostring loppuu
class hello_world_resource : public httpserver::http_resource
{
public:
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&);
std::string data01, data02;
};
// Using the render method you are able to catch each type of request you receive
const std::shared_ptr<httpserver::http_response> hello_world_resource::render(const httpserver::http_request& req)
{
std::cout << std::endl;
std::string datapar01 = req.get_arg("data01");
std::string datapar02 = req.get_arg("data02");
std::string datapar03 = req.get_arg("data03");
std::string datapar04 = req.get_arg("data04");
std::string datapar05 = req.get_arg("data05");
std::string datapar06 = req.get_arg("data06");
std::string datapar07 = req.get_arg("data07");
std::string datapar08 = req.get_arg("data08");
std::string datapar09 = req.get_arg("data09");
std::string datapar10 = req.get_arg("data10");
std::string datapar11 = req.get_arg("data11");
std::cout << "Now data 1 is:" << datapar01 << std::endl;
std::cout << "Now data 2 is:" << datapar02 << std::endl;
std::cout << "Now data 3 is:" << datapar03 << std::endl;
std::string windowstring1 = filetostring("seurantakaavake01.html");
//std::string windowstring2 = "pvm.setAttribute(\"value\" , "\"" + datapar01 + "\"); ";
std::string windowstring2 =
"pvm.setAttribute(\"value\" ,\"" + datapar01 + "\"); ";
std::string windowstring3 = " } </script></body></html> \n";
std::string ws = windowstring1 + windowstring2 + windowstring3;
return std::shared_ptr<httpserver::http_response>(new
httpserver::string_response(ws, 200));
}
int main()
{ // porttinumero ja säiemäärä parametreina
httpserver::webserver ws = httpserver::create_webserver(8080).start_method(httpserver::http::http_utils::INTERNAL_SELECT).max_threads(5);
hello_world_resource hwr;
ws.register_resource("/hello.html", &hwr, true);
ws.start(true); // blocking on true, non blocking olisi false
return 0;
}
g++ libhttpserver02.cpp -o libhttpserver02 -Wall -pedantic -L/usr/local/lib -lhttpserver -lmicrohttpd -pthread -std=c++17
#include <iostream>
int main()
{
std::string str4 =
" <form action=" http://localhost:8080/hello" method="post"> " ";
std::cout << str4 << std::endl;
return 0;
}
#include <iostream>
#include "/home/k1/Documents/my_libhttpserver/libhttpserveri/libhttpserver/src/httpserver.hpp"
class hello_world_resource : public httpserver::http_resource
{
public:
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&);
std::string data01, data02;
};
// Using the render method you are able to catch each type of request you receive
const std::shared_ptr<httpserver::http_response> hello_world_resource::render(const httpserver::http_request& req)
{
std::cout << std::endl;
std::string datapar01 = req.get_arg("data01");
std::string datapar02 = req.get_arg("data02");
std::cout << "Now data 1 is:" << datapar01 << std::endl;
std::cout << "Now data 2 is:" << datapar02 << std::endl;
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("Hello World!!!", 200));
}
int main()
{ // porttinumero ja säiemäärä parametreina
httpserver::webserver ws = httpserver::create_webserver(8080).start_method(httpserver::http::http_utils::INTERNAL_SELECT).max_threads(5);
hello_world_resource hwr;
ws.register_resource("/hello", &hwr, true);
ws.start(true); // blocking on true, non blocking olisi false
return 0;
}
g++ libhttpserver02.cpp -o libhttpserver02 -Wall -pedantic -L/usr/local/lib -lhttpserver -lmicrohttpd -pthread -std=c++17
<!DOCTYPE html>
<html>
<body>
<h1>Lomakekokeilu post- ja get-menetelmät</h1>
<form action=" http://localhost:8080/hello" method="post">
<label for="fname1">First name:</label>
<input type="text" id="fnameid1" name="data01"><br><br>
<label for="fnmame2">Last data:</label>
<input type="text" id="dataid2" name="data02"><br><br>
<button type="submit">Lähetä post</button>
</form>
<br /> <br/ > <br/ > <br/ >
<!-- get ei lähettäne ääkkösiä oikein --!>
<form action=" http://localhost:8080/hello" method="GET">
<label for="fname">First name:</label>
<input type="text" id="fname" name="data01"><br><br>
<label for="data">Last data:</label>
<input type="text" id="data02" name="data02"><br><br>
<button type="submit">Lähetä get</button>
</form>
" "
<br/ > <br/ > <br/ > <br/ >
<form>
<button type="submit" formaction="http://localhost:8080/hello"
method="post">Lähetä tyhjä post
</button>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>Lomakekokeilu post- ja get-menetelmät</h1>
<form action=" http://localhost:8080/hello" method="post">
<label for="fname1">First name:</label>
<input type="text" id="fnameid1" name="data1"><br><br>
<label for="fnmame2">Last data:</label>
<input type="text" id="dataid2" name="data"><br><br>
<button type="submit">Lähetä post</button>
</form>
</br> <br/> <br/> <br/>
<!-- get ei lähettäne ääkkösiä oikein --!>
<form action=" http://localhost:8080/hello" method="GET">
<label for="fname">First name:</label>
<input type="text" id="fname" name="data"><br><br>
<label for="data">Last data:</label>
<input type="text" id="data" name="data"><br><br>
<button type="submit">Lähetä get</button>
</form>
" "
<br/> <br/> <br/> <br/>
<form>
<button type="submit" formaction="http://localhost:8080/hello"
method="post">Lähetä tyhjä post
</button>
</form>
</body>
</html>
#include <iostream>
//// /home/k1/Documents/my_libhttpserver/libhttpserveri/libhttpserver/
#include "/home/k1/Documents/my_libhttpserver/libhttpserveri/libhttpserver/src/httpserver.hpp"
class hello_world_resource : public httpserver::http_resource {
public:
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&);
void set_some_data(const std::string &s) {data = s;}
std::string data;
};
// Using the render method you are able to catch each type of request you receive
const std::shared_ptr<httpserver::http_response> hello_world_resource::render(const httpserver::http_request& req) {
// It is possible to store data inside the resource object that can be altered through the requests
std::cout << "Data was: " << data << std::endl;
std::string datapar = req.get_arg("data");
set_some_data(datapar == "" ? "no data passed!!!" : datapar);
std::cout << "Now data is:" << data << std::endl;
// It is possible to send a response initializing an http_string_response that reads the content to send in response from a string.
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("Hello World!!!", 200));
}
int main() {
// It is possible to create a webserver passing a great number of parameters. In this case we are just passing the port and the number of thread running.
httpserver::webserver ws = httpserver::create_webserver(8080).start_method(httpserver::http::http_utils::INTERNAL_SELECT).max_threads(5);
hello_world_resource hwr;
// This way we are registering the hello_world_resource to answer for the endpoint
// "/hello". The requested method is called (if the request is a GET we call the render_GET
// method. In case that the specific render method is not implemented, the generic "render"
// method is called.
ws.register_resource("/hello", &hwr, true);
// This way we are putting the created webserver in listen. We pass true in order to have
// a blocking call; if we want the call to be non-blocking we can just pass false to the method.
ws.start(true);
return 0;
}
g++ libhttpserver01.cpp -o libhttpserver01 -Wall -pedantic -L/usr/local/lib -lhttpserver -lmicrohttpd -pthread -std=c++17
<!DOCTYPE html>
<html>
<body>
<h1>Lomakekokeilu post- ja get-menetelmät</h1>
<form action=" http://localhost:8080/hello" method="post">
<label for="fname1">First name:</label>
<input type="text" id="fnameid1" name="data"><br><br>
<label for="fnmame2">Last data:</label>
<input type="text" id="dataid2" name="data"><br><br>
<button type="submit">Lähetä post</button>
</form>
</br> </br> </br> </br>
<!-- get ei lähettäne ääkkösiä oikein --!>
<form action=" http://localhost:8080/hello" method="GET">
<label for="fname">First name:</label>
<input type="text" id="fname" name="data"><br><br>
<label for="data">Last data:</label>
<input type="text" id="data" name="data"><br><br>
<button type="submit">Lähetä get</button>
</form>
" "
</br> </br> </br> </br>
<form>
<button type="submit" formaction="http://localhost:8080/hello"
method="post">Lähetä tyhjä post
</button>
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<h1>Lomakekokeilu post- ja get-menetelmät</h1>
<form action=" http://localhost:8080/hello" method="post">
<label for="fname1">First name:</label>
<input type="text" id="fnameid1" name="data"><br><br>
<label for="fnmame2">Last data:</label>
<input type="text" id="dataid2" name="data"><br><br>
<button type="submit">Lähetä post</button>
</form>
</br> </br> </br> </br>
<!-- get ei lähettäne ääkkösiä oikein --!>
<form action=" http://localhost:8080/hello" method="GET">
<label for="fname">First name:</label>
<input type="text" id="fname" name="data"><br><br>
<label for="data">Last data:</label>
<input type="text" id="data" name="data"><br><br>
<button type="submit">Lähetä get</button>
</form>
" "
</br> </br> </br> </br>
<form>
<button type="submit" formaction="http://localhost:8080/hello"
method="post">Lähetä tyhjä post
</button>
</form>
</body>
</html>
#include <iostream>
#include "/home/k1/Documents/my_libhttpserver/libhttpserveri/libhttpserver/src/httpserver.hpp"
class hello_world_resource : public httpserver::http_resource {
public:
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&);
void set_some_data(const std::string &s) {data = s;}
std::string data;
};
// Using the render method you are able to catch each type of request you receive
const std::shared_ptr<httpserver::http_response> hello_world_resource::render(const httpserver::http_request& req) {
// It is possible to store data inside the resource object that can be altered through the requests
std::cout << "Data was: " << data << std::endl;
std::string datapar = req.get_arg("data");
set_some_data(datapar == "" ? "no data passed!!!" : datapar);
std::cout << "Now data is:" << data << std::endl;
// It is possible to send a response initializing an http_string_response that reads the content to send in response from a string.
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("Hello World!!!", 200));
}
int main() {
// It is possible to create a webserver passing a great number of parameters. In this case we are just passing the port and the number of thread running.
httpserver::webserver ws = httpserver::create_webserver(8080).start_method(httpserver::http::http_utils::INTERNAL_SELECT).max_threads(5);
hello_world_resource hwr;
// This way we are registering the hello_world_resource to answer for the endpoint
// "/hello". The requested method is called (if the request is a GET we call the render_GET
// method. In case that the specific render method is not implemented, the generic "render"
// method is called.
ws.register_resource("/hello", &hwr, true);
// This way we are putting the created webserver in listen. We pass true in order to have
// a blocking call; if we want the call to be non-blocking we can just pass false to the method.
ws.start(true);
return 0;
}
g++ libhttpserver01.cpp -o libhttpserver01 -Wall -pedantic -L/usr/local/lib -lhttpserver -lmicrohttpd -pthread -std=c++17
$ curl -X POST -F 'name=linuxize' -F 'data=ööö pp öö åååååååh' http://localhost:8080/hello
Data was: no data passed!!!
Now data is:ööö pp öö åååååååh
<!DOCTYPE html>
<html>
<body>
<h1>Lomakekokeilu post- ja get-menetelmät</h1>
<form action=" http://localhost:8080/hello" method="post">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="data">Last data:</label>
<input type="text" id="data" name="datatekstiä"><br><br>
<button type="submit">Lähetä post</button>
</form>
</br> </br> </br> </br>
<form action=" http://localhost:8080/hello" method="get">
<label for="fname">First name:</label>
<input type="text" id="fname" name="fname"><br><br>
<label for="data">Last data:</label>
<input type="text" id="data" name="datatekstiä"><br><br>
<button type="submit">Lähetä get</button>
</form>
" "
</br> </br> </br> </br>
<form>
<button type="submit" formaction="http://localhost:8080/hello"
method="post">Lähetä tyhjä post
</button>
</form>
</body>
</html>
Data was: no data passed!!!
Now data is:no data passed!!!
$ find /usr/local/lib/ -name libhttpserver.so.0
/usr/local/lib/libhttpserver.so.0
echo $LD_LIBRARY_PATH
$ g++ libhttpserver01.cpp -o libhttpserver01 -Wall -pedantic -L/usr/local/lib -lhttpserver -lmicrohttpd -pthread -std=c++17
$ ./libhttpserver01
./libhttpserver01: error while loading shared libraries: libhttpserver.so.0: cannot open shared object file: No such file or directory
$ g++ lbhttpser01.cpp -o lbhttpser01 -Wall -pedantic -L/usr/local/lib -pthread -std=c++17
/usr/bin/ld: /tmp/ccTd04do.o: in function `hello_world_resource::render(httpserver::http_request const&)':
lbhttpser01.cpp:(.text+0xb6): undefined reference to `httpserver::http_request::get_arg(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const'
/usr/bin/ld: lbhttpser01.cpp:(.text+0x1e9): undefined reference to `httpserver::http::http_utils::text_plain'
/usr/bin/ld: /tmp/ccTd04do.o: in function `main':
lbhttpser01.cpp:(.text+0x41b): undefined reference to `httpserver::webserver::webserver(httpserver::create_webserver const&)'
/usr/bin/ld: lbhttpser01.cpp:(.text+0x487): undefined reference to `httpserver::webserver::register_resource(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, httpserver::http_resource*, bool)'
/usr/bin/ld: lbhttpser01.cpp:(.text+0x4b9): undefined reference to `httpserver::webserver::start(bool)'
/usr/bin/ld: lbhttpser01.cpp:(.text+0x4dc): undefined reference to `httpserver::webserver::~webserver()'
/usr/bin/ld: lbhttpser01.cpp:(.text+0x575): undefined reference to `httpserver::webserver::~webserver()' ..... jne
#include <iostream>
#include "/home/k1/Documents/my_libhttpserver/libhttpserveri/libhttpserver/src/httpserver.hpp"
class hello_world_resource : public httpserver::http_resource {
public:
const std::shared_ptr<httpserver::http_response> render(const httpserver::http_request&);
void set_some_data(const std::string &s) {data = s;}
std::string data;
};
// Using the render method you are able to catch each type of request you receive
const std::shared_ptr<httpserver::http_response> hello_world_resource::render(const httpserver::http_request& req) {
// It is possible to store data inside the resource object that can be altered through the requests
std::cout << "Data was: " << data << std::endl;
std::string datapar = req.get_arg("data");
set_some_data(datapar == "" ? "no data passed!!!" : datapar);
std::cout << "Now data is:" << data << std::endl;
// It is possible to send a response initializing an http_string_response that reads the content to send in response from a string.
return std::shared_ptr<httpserver::http_response>(new httpserver::string_response("Hello World!!!", 200));
}
int main() {
// It is possible to create a webserver passing a great number of parameters. In this case we are just passing the port and the number of thread running.
httpserver::webserver ws = httpserver::create_webserver(8080).start_method(httpserver::http::http_utils::INTERNAL_SELECT).max_threads(5);
hello_world_resource hwr;
// This way we are registering the hello_world_resource to answer for the endpoint
// "/hello". The requested method is called (if the request is a GET we call the render_GET
// method. In case that the specific render method is not implemented, the generic "render"
// method is called.
ws.register_resource("/hello", &hwr, true);
// This way we are putting the created webserver in listen. We pass true in order to have
// a blocking call; if we want the call to be non-blocking we can just pass false to the method.
ws.start(true);
return 0;
}
sudo apt install aclocal
sudo apt install automake
sudo apt install git
sudo apt install libtool
sudo apt install libmicrohttpd-dev
sudo apt install libgnutls28-dev
sudo apt autoremove
d /home/koti/Documents/koti_libhttpserver
git clone https://github.com/git-guides/git-clone
git config –global user.name "oma-git-tunnus"
mkdir git-noudot
cd git-noudot
git init
git remote add origin https://github.com/oma-git-tunnus/.git/
git clone https://github.com/etr/libhttpserver
ls
cd libhttpserver
ls
./bootstrap
./configure
make