Skip to content

Commit 92dc020

Browse files
committed
Fix default ports
1 parent 9ce8216 commit 92dc020

File tree

5 files changed

+154
-3
lines changed

5 files changed

+154
-3
lines changed

.gitignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
*.plist
44
tests/test
55
tests/*.o
6-
tests/_test_pdf2printable_16x9_*
7-
tests/_test_pdf2printable_4x3_*
6+
tests/_test*
87
ppm2pwg
98
pwg2ppm
109
pdf2printable

lib/curlrequester.cpp

+23-1
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,30 @@ void CurlIppPosterBase::setCompression(Compression compression)
156156
deflateInit2(&_zstrm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, level, 7, Z_DEFAULT_STRATEGY);
157157
}
158158

159+
std::string http_url(std::string str)
160+
{
161+
Url url(str);
162+
if(url.getScheme() == "ipp")
163+
{
164+
url.setScheme("http");
165+
if(!url.getPort())
166+
{
167+
url.setPort(631);
168+
}
169+
}
170+
else if(url.getScheme() == "ipps")
171+
{
172+
url.setScheme("https");
173+
if(!url.getPort())
174+
{
175+
url.setPort(443);
176+
}
177+
}
178+
return url.toStr();
179+
}
180+
159181
CurlIppPosterBase::CurlIppPosterBase(std::string addr, bool ignoreSslErrors)
160-
: CurlRequester("http"+addr.erase(0,3), ignoreSslErrors)
182+
: CurlRequester(http_url(addr), ignoreSslErrors)
161183
{
162184
_canWrite.unlock();
163185
_canRead.lock();

lib/curlrequester.h

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "array.h"
55
#include "bytestream.h"
66
#include "lthread.h"
7+
#include "url.h"
78

89
#include <curl/curl.h>
910
#include <zlib.h>

lib/url.h

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#ifndef URL_H
2+
#define URL_H
3+
4+
#include <string>
5+
#include <regex>
6+
7+
class Url
8+
{
9+
public:
10+
Url() = delete;
11+
Url(std::string str)
12+
{
13+
match(str);
14+
}
15+
16+
Url& operator=(std::string str)
17+
{
18+
match(str);
19+
return *this;
20+
}
21+
22+
operator std::string()
23+
{
24+
return toStr();
25+
}
26+
27+
std::string toStr()
28+
{
29+
std::string maybePort = _port != 0 ? ":" + std::to_string(_port) : "";
30+
return _scheme + "://" + _host + maybePort + _path;
31+
}
32+
33+
std::string getScheme()
34+
{
35+
return _scheme;
36+
}
37+
38+
void setScheme(std::string scheme)
39+
{
40+
_scheme = scheme;
41+
}
42+
43+
std::string getHost()
44+
{
45+
return _host;
46+
}
47+
48+
void setHost(std::string host)
49+
{
50+
_host = host;
51+
}
52+
53+
uint16_t getPort()
54+
{
55+
return _port;
56+
}
57+
58+
void setPort(uint16_t port)
59+
{
60+
_port = port;
61+
}
62+
63+
std::string getPath()
64+
{
65+
return _path;
66+
}
67+
68+
void setPath(std::string path)
69+
{
70+
_path = path;
71+
}
72+
73+
private:
74+
75+
void match(std::string str)
76+
{
77+
static const std::regex regex("(([a-z]+)://)([a-z0-9-.]+)(:([0-9]+))?(/.*)?$");
78+
std::smatch match;
79+
if(!std::regex_match(str, match, regex))
80+
{
81+
throw(std::logic_error("Invalid url"));
82+
}
83+
_scheme = match[2];
84+
_host = match[3];
85+
_port = match[5] == "" ? 0 : stoul(match[5]);
86+
_path = match[6];
87+
}
88+
89+
std::string _scheme;
90+
std::string _host;
91+
uint16_t _port;
92+
std::string _path;
93+
};
94+
95+
#endif // URL_H

tests/test.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "ippprinter.h"
1010
#include "ippprintjob.h"
1111
#include "json11.hpp"
12+
#include "url.h"
1213
#include <cstring>
1314
#include <filesystem>
1415
using namespace std;
@@ -2437,3 +2438,36 @@ TEST(malicious_dns)
24372438
ASSERT_THROW(get_addr(bts), logic_error);
24382439

24392440
}
2441+
2442+
TEST(url)
2443+
{
2444+
Url url("ipp://myprinter:631/ipp/print");
2445+
ASSERT(url.getScheme() == "ipp");
2446+
ASSERT(url.getHost() == "myprinter");
2447+
ASSERT(url.getPort() == 631);
2448+
ASSERT(url.getPath() == "/ipp/print");
2449+
ASSERT(url.toStr() == "ipp://myprinter:631/ipp/print");
2450+
2451+
url = "ipp://myprinter/ipp/print";
2452+
ASSERT(url.getScheme() == "ipp");
2453+
ASSERT(url.getHost() == "myprinter");
2454+
ASSERT(url.getPath() == "/ipp/print");
2455+
ASSERT(url.toStr() == "ipp://myprinter/ipp/print");
2456+
2457+
ASSERT_FALSE(url.getPort());
2458+
url.setPort(631);
2459+
ASSERT(url.toStr() == "ipp://myprinter:631/ipp/print");
2460+
2461+
url = "ipp://myprinter";
2462+
ASSERT(url.getScheme() == "ipp");
2463+
ASSERT(url.getHost() == "myprinter");
2464+
ASSERT(url.getPath() == "");
2465+
ASSERT(url.toStr() == "ipp://myprinter");
2466+
2467+
url.setScheme("ipps");
2468+
url.setHost("yourprinter");
2469+
url.setPort(666);
2470+
url.setPath("/foo/bar");
2471+
ASSERT(url.toStr() == "ipps://yourprinter:666/foo/bar");
2472+
2473+
}

0 commit comments

Comments
 (0)