mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2025-05-10 09:43:51 +00:00
Changed to take host-name instead of address.
This commit is contained in:
parent
c5c1c4d412
commit
3fbd3afb6c
@ -23,14 +23,10 @@ int main(void)
|
|||||||
});
|
});
|
||||||
|
|
||||||
svr.get("/item/:name", [](Context& cxt) {
|
svr.get("/item/:name", [](Context& cxt) {
|
||||||
try {
|
cxt.response.body = cxt.request.params.at("name");
|
||||||
cxt.response.body = cxt.request.params.at("name");
|
|
||||||
} catch (...) {
|
|
||||||
// Error...
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
svr.run("0.0.0.0", 1234);
|
svr.run("localhost", 1234);
|
||||||
}
|
}
|
||||||
|
|
||||||
// vim: et ts=4 sw=4 cin cino={1s ff=unix
|
// vim: et ts=4 sw=4 cin cino={1s ff=unix
|
||||||
|
46
httpsvrkit.h
46
httpsvrkit.h
@ -12,24 +12,15 @@
|
|||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <ws2tcpip.h>
|
|
||||||
|
|
||||||
typedef unsigned __int16 uint16_t;
|
|
||||||
typedef SOCKET socket_t;
|
typedef SOCKET socket_t;
|
||||||
|
|
||||||
int inet_aton(const char* strptr, struct in_addr* addrptr)
|
|
||||||
{
|
|
||||||
unsigned long addr = inet_addr(strptr);
|
|
||||||
if (addr == ULONG_MAX)
|
|
||||||
return 0;
|
|
||||||
addrptr->s_addr = addr;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <netdb.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
|
||||||
typedef int socket_t;
|
typedef int socket_t;
|
||||||
#endif
|
#endif
|
||||||
@ -73,10 +64,10 @@ public:
|
|||||||
Server();
|
Server();
|
||||||
~Server();
|
~Server();
|
||||||
|
|
||||||
void get(const std::string& pattern, Handler handler);
|
void get(const char* pattern, Handler handler);
|
||||||
void post(const std::string& pattern, Handler handler);
|
void post(const char* pattern, Handler handler);
|
||||||
|
|
||||||
bool run(const std::string& ipaddr, int port);
|
bool run(const char* ipaddr_or_hostname, int port);
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -88,7 +79,7 @@ private:
|
|||||||
|
|
||||||
// Implementation
|
// Implementation
|
||||||
|
|
||||||
inline socket_t create_socket(const std::string& ipaddr, int port)
|
inline socket_t create_server_socket(const const char* ipaddr_or_hostname, int port)
|
||||||
{
|
{
|
||||||
// Create a server socket
|
// Create a server socket
|
||||||
socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
|
socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||||
@ -100,16 +91,21 @@ inline socket_t create_socket(const std::string& ipaddr, int port)
|
|||||||
int opt = 1;
|
int opt = 1;
|
||||||
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt));
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char*)&opt, sizeof(opt));
|
||||||
|
|
||||||
// Bind the socket to the given address
|
// Get a host entry info
|
||||||
struct sockaddr_in addr;
|
struct hostent* hp;
|
||||||
addr.sin_family = AF_INET;
|
if (!(hp = gethostbyname(ipaddr_or_hostname))) {
|
||||||
addr.sin_port = htons((uint16_t)port);
|
|
||||||
|
|
||||||
if (inet_aton(ipaddr.c_str(), &addr.sin_addr) <= 0) {
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bind the socket to the given address
|
||||||
|
struct sockaddr_in addr;
|
||||||
|
memset(&addr, 0, sizeof(addr));
|
||||||
|
memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons(port);
|
||||||
|
|
||||||
if (::bind(sock, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
|
if (::bind(sock, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
|
||||||
|
puts("(error)\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,19 +152,19 @@ inline Server::~Server()
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Server::get(const std::string& pattern, Handler handler)
|
inline void Server::get(const char* pattern, Handler handler)
|
||||||
{
|
{
|
||||||
handlers_.insert(std::make_pair(pattern, handler));
|
handlers_.insert(std::make_pair(pattern, handler));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Server::post(const std::string& pattern, Handler handler)
|
inline void Server::post(const char* pattern, Handler handler)
|
||||||
{
|
{
|
||||||
handlers_.insert(std::make_pair(pattern, handler));
|
handlers_.insert(std::make_pair(pattern, handler));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Server::run(const std::string& ipaddr, int port)
|
inline bool Server::run(const const char*ipaddr_or_hostname, int port)
|
||||||
{
|
{
|
||||||
sock_ = create_socket(ipaddr, port);
|
sock_ = create_server_socket(ipaddr_or_hostname, port);
|
||||||
if (sock_ == -1) {
|
if (sock_ == -1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user