std::string encode_field(str_ref v, bool add_br) { std::string r; r.reserve(v.size() << 1); while (v) { if (boost::istarts_with(v, "ftp://") || boost::istarts_with(v, "http://") || boost::istarts_with(v, "https://") || boost::istarts_with(v, "mailto:")) { size_t p = 0; while (p < v.size() && !isspace(v[p] & 0xff) && v[p] != '\"' && v[p] != '<' && v[p] != '>' && v[p] != '[' && v[p] != ']') p++; if (v[p - 1] == '!' || v[p - 1] == ',' || v[p - 1] == '.' || v[p - 1] == '?') p--; if (v[p - 1] == ')') p--; str_ref url = v.substr(0, p); if (boost::istarts_with(v, "ftp.")) r += web_link(url, "ftp://" + url.s()); else if (boost::istarts_with(v, "www.")) r += web_link(url, "http://" + url.s()); else r += web_link(boost::istarts_with(v, "mailto:") ? url.substr(7) : url, url); while (p--) v.pop_front(); } else { switch (v.front()) { case '\n': r += add_br ? "<br>" : " "; break; case '\r': break; case '&': r += "&"; break; case '<': r += "<"; break; default: r += v.front(); } v.pop_front(); } } return r; }
static int send_recv(const string& host, int port, str_ref s0, string& d) { Csocket s; s.open(SOCK_STREAM, true); if (s == -1) { d += "unable to create socket: " + Csocket::error2a(WSAGetLastError()); return 1; } d = (boost::format("server: %s:%d (%s)\r\n") % host % port % Csocket::inet_ntoa(Csocket::get_host(host))).str(); if (s.connect(Csocket::get_host(host), htons(port))) { d += "unable to connect: " + Csocket::error2a(WSAGetLastError()); return 1; } if (s0.size() != s.send(s0)) { d += "unable to send: " + Csocket::error2a(WSAGetLastError()); return 1; } array<char, 4 << 10> d0; while (int e = s.recv(d0)) { if (e == SOCKET_ERROR) { d += "unable to receive: " + Csocket::error2a(WSAGetLastError()); return 1; } d.append(d0.data(), e); } return 0; }
std::string encode_text(str_ref v, bool add_quote_class) { std::string r; r.reserve(v.size() << 1); while (v) { str_ref line = read_until(v, '\n'); r += add_quote_class && boost::istarts_with(line, "> ") ? "<span class=quote>" + encode_field(line) + "</span>" : encode_field(line); r += "<br>"; } return r; }
static std::string web_encode(str_ref v) { std::string d; d.reserve(v.size() << 1); while (v) { switch (v.front()) { case '"': d += """; break; case '&': d += "&"; break; case '<': d += "<"; break; default: d += v.front(); } v.pop_front(); } return d; }
int Cbvalue::write(str_ref s) { return write(s.data(), s.size()); }
static bool operator==(str_ref a, const char* b) { return a.size() == strlen(b) && !memcmp(a.data(), b, a.size()); }
int Csocket::sendto(str_ref s, const sockaddr* a, socklen_t cb_a) const { return ::sendto(*this, s.data(), s.size(), MSG_NOSIGNAL, a, cb_a); }
int Csocket::send(str_ref s) const { return ::send(*this, s.data(), s.size(), MSG_NOSIGNAL); }