int main(int argc,char *argv[]) { if (argc < 2) return -1; std::string str; std::string str2; Base64 b; if (!strcmp(argv[1],"-file")) { if (argc < 3) return -2; FILE *fil; if ((fil = fopen(argv[2],"rt")) != NULL) { b.encode(fil, str); fclose(fil); } printf("File:\n%s\n--End of File\n",str.c_str()); b.decode(str, str2); printf("Content:\n%s\n--End of Content\n",str2.c_str()); } else { b.encode(argv[1], strlen(argv[1]), str); printf("'%s' ==> '%s'",argv[1], str.c_str()); b.decode(str, str2); printf(" ==> '%s'\n",str2.c_str()); } }
void HttpdSocket::Send64(const std::string& str64, const std::string& type) { Base64 bb; if (!strcasecmp(m_start.c_str(), m_if_modified_since.c_str())) { SetStatus("304"); SetStatusText("Not Modified"); SendResponse(); } else { size_t len = bb.decode_length(str64); unsigned char *buf = new unsigned char[len]; SetStatus("200"); SetStatusText("OK"); AddResponseHeader("Content-length", Utility::l2string( (long)len) ); AddResponseHeader("Content-type", type ); AddResponseHeader("Last-modified", m_start); SendResponse(); bb.decode(str64, buf, len); SendBuf( (char *)buf, len); delete[] buf; } }
int main() { using std::cout; using std::endl; using namespace thrift::test::debug; using apache::thrift::transport::TMemoryBuffer; using apache::thrift::protocol::TJSONProtocol; OneOfEach ooe; ooe.im_true = true; ooe.im_false = false; ooe.a_bite = 0x7f; ooe.integer16 = 27000; ooe.integer32 = 1<<24; ooe.integer64 = (uint64_t)6000 * 1000 * 1000; ooe.double_precision = M_PI; ooe.some_characters = "JSON THIS! \"\1"; ooe.zomg_unicode = "\xd7\n\a\t"; ooe.base64 = "\1\2\3\255"; cout << apache::thrift::ThriftJSONString(ooe) << endl << endl; Nesting n; n.my_ooe = ooe; n.my_ooe.integer16 = 16; n.my_ooe.integer32 = 32; n.my_ooe.integer64 = 64; n.my_ooe.double_precision = (std::sqrt(5.0)+1)/2; n.my_ooe.some_characters = ":R (me going \"rrrr\")"; n.my_ooe.zomg_unicode = "\xd3\x80\xe2\x85\xae\xce\x9d\x20" "\xd0\x9d\xce\xbf\xe2\x85\xbf\xd0\xbe\xc9\xa1\xd0\xb3\xd0\xb0\xcf\x81\xe2\x84\x8e" "\x20\xce\x91\x74\x74\xce\xb1\xe2\x85\xbd\xce\xba\xc7\x83\xe2\x80\xbc"; n.my_bonk.type = 31337; n.my_bonk.message = "I am a bonk... xor!"; cout << apache::thrift::ThriftJSONString(n) << endl << endl; HolyMoley hm; hm.big.push_back(ooe); hm.big.push_back(n.my_ooe); hm.big[0].a_bite = 0x22; hm.big[1].a_bite = 0x33; std::vector<std::string> stage1; stage1.push_back("and a one"); stage1.push_back("and a two"); hm.contain.insert(stage1); stage1.clear(); stage1.push_back("then a one, two"); stage1.push_back("three!"); stage1.push_back("FOUR!!"); hm.contain.insert(stage1); stage1.clear(); hm.contain.insert(stage1); std::vector<Bonk> stage2; hm.bonks["nothing"] = stage2; stage2.resize(stage2.size()+1); stage2.back().type = 1; stage2.back().message = "Wait."; stage2.resize(stage2.size()+1); stage2.back().type = 2; stage2.back().message = "What?"; hm.bonks["something"] = stage2; stage2.clear(); stage2.resize(stage2.size()+1); stage2.back().type = 3; stage2.back().message = "quoth"; stage2.resize(stage2.size()+1); stage2.back().type = 4; stage2.back().message = "the raven"; stage2.resize(stage2.size()+1); stage2.back().type = 5; stage2.back().message = "nevermore"; hm.bonks["poe"] = stage2; cout << apache::thrift::ThriftJSONString(hm) << endl << endl; boost::shared_ptr<TMemoryBuffer> buffer(new TMemoryBuffer()); boost::shared_ptr<TJSONProtocol> proto(new TJSONProtocol(buffer)); cout << "Testing ooe" << endl; ooe.write(proto.get()); OneOfEach ooe2; ooe2.read(proto.get()); assert(ooe == ooe2); cout << "Testing hm" << endl; hm.write(proto.get()); HolyMoley hm2; hm2.read(proto.get()); assert(hm == hm2); hm2.big[0].a_bite = 0x00; assert(hm != hm2); Doubles dub; dub.nan = HUGE_VAL/HUGE_VAL; dub.inf = HUGE_VAL; dub.neginf = -HUGE_VAL; dub.repeating = 10.0/3.0; dub.big = 1E+305; dub.small = 1E-305; dub.zero = 0.0; dub.negzero = -0.0; cout << apache::thrift::ThriftJSONString(dub) << endl << endl; cout << "Testing base" << endl; Base64 base; base.a = 123; base.b1 = "1"; base.b2 = "12"; base.b3 = "123"; base.b4 = "1234"; base.b5 = "12345"; base.b6 = "123456"; base.write(proto.get()); Base64 base2; base2.read(proto.get()); assert(base == base2); return 0; }
void ResolvSocket::OnDetached() { #ifndef _WIN32 if (m_query == "getaddrinfo") { struct addrinfo hints; struct addrinfo *res; memset(&hints, 0, sizeof(hints)); hints.ai_flags |= AI_CANONNAME; int n = getaddrinfo(m_data.c_str(), NULL, &hints, &res); if (!n) { /* struct addrinfo { int ai_flags; int ai_family; // PF_INET, PF_INET6 int ai_socktype; int ai_protocol; size_t ai_addrlen; struct sockaddr *ai_addr; char *ai_canonname; struct addrinfo *ai_next; }; */ while (res) { Send("Flags: " + Utility::l2string(res -> ai_flags) + "\n"); Send("Family: " + Utility::l2string(res -> ai_family) + "\n"); Send("Socktype: " + Utility::l2string(res -> ai_socktype) + "\n"); Send("Protocol: " + Utility::l2string(res -> ai_protocol) + "\n"); Send("Addrlen: " + Utility::l2string(res -> ai_addrlen) + "\n"); std::string tmp; Base64 bb; bb.encode( (unsigned char *)res -> ai_addr, res -> ai_addrlen, tmp, false); Send("Address: " + tmp + "\n"); // base64-encoded sockaddr Send("Canonname: "); Send( res -> ai_canonname ); Send("\n"); Send("\n"); // res = res -> ai_next; } freeaddrinfo(res); } else { std::string error = "Error: "; error += gai_strerror(n); Send( error + "\n" ); Send("\n"); } } else #endif // _WIN32 if (m_query == "gethostbyname") { struct hostent *h = gethostbyname(m_data.c_str()); if (h) { char slask[1000]; sprintf(slask, "Name: %s\n", h -> h_name); Send( slask ); size_t i = 0; while (h -> h_aliases[i]) { sprintf(slask, "Alias: %s\n", h -> h_aliases[i]); Send( slask ); i++; } sprintf(slask, "AddrType: %d\n", h -> h_addrtype); Send( slask ); sprintf(slask, "Length: %d\n", h -> h_length); Send( slask ); i = 0; while (h -> h_addr_list[i]) { // let's assume 4 byte IPv4 addresses char ip[40]; *ip = 0; for (int j = 0; j < 4; j++) { if (*ip) strcat(ip,"."); sprintf(ip + strlen(ip),"%u",(unsigned char)h -> h_addr_list[i][j]); } sprintf(slask, "A: %s\n", ip); Send( slask ); i++; } } else { Send("Failed\n"); } Send( "\n" ); } else if (m_query == "gethostbyaddr") { Send("Not Implemented\n\n"); } else { std::string msg = "Unknown query type: " + m_query; Handler().LogError(this, "OnDetached", 0, msg); Send("Unknown\n\n"); } SetCloseAndDelete(); }