void sslTest2() { tracef("ssl test 2 begin: ssl send data."); Context::Ptr pContext = new Context(Context::TLSV1_CLIENT_USE, "", Context::VERIFY_NONE); SecureStreamSocket sss(pContext); SocketAddress sa("127.0.0.1", 12222); sss.connect(sa, Timespan(3, 0)); DynamicStruct ds; ds["type"] = "request"; ds["action"] = "server.token"; Timestamp t; UInt64 tms = t.epochMicroseconds(); char tms_str[32]; snprintf(tms_str, 31, "%llu", tms); std::string key = "alpha2015"; key += tms_str; MD5Engine md5; md5.update(key); const DigestEngine::Digest& digest = md5.digest(); std::string md5key = DigestEngine::digestToHex(digest); DynamicStruct param; param["key"] = md5key; param["timestamp"] = tms_str; param["dev_name"] = "lock1"; param["dev_type"] = "sc-01"; param["uuid"] = "SC00000001"; ds["param"] = param; tracef("data send: %s.", ds.toString().c_str()); sss.sendBytes(ds.toString().c_str(), ds.toString().length()); sss.close(); tracef("socket closed."); tracef("ssl test 2 finished.\n"); }
void regTest2() { tracef("reg test 2 begin: reg send data."); StreamSocket ss; SocketAddress sa("127.0.0.1", 13333); ss.connect(sa, Timespan(3, 0)); DynamicStruct ds; ds["type"] = "request"; ds["action"] = "server.register"; DynamicStruct param; param["token"] = "1234567890"; param["uuid"] = "SC000000001"; ds["param"] = param; ss.sendBytes(ds.toString().c_str(), ds.toString().length()); tracef("reg data send: %s.", ds.toString().c_str()); ss.close(); tracef("socket closed."); tracef("register test 2 finished.\n"); }
void regTest3() { tracef("reg test 3 begin: reg send and receive data, register fail"); StreamSocket ss; SocketAddress sa("127.0.0.1", 13333); ss.connect(sa, Timespan(3, 0)); DynamicStruct ds; ds["type"] = "request"; ds["action"] = "server.register"; DynamicStruct param; param["token"] = "1234567890"; param["uuid"] = "SC000000001"; ds["param"] = param; ss.sendBytes(ds.toString().c_str(), ds.toString().length()); tracef("reg data send: %s.", ds.toString().c_str()); char buf[1024] = {0, }; ss.receiveBytes(buf, 1024); tracef("receive bytes: %s.", buf); tracef("socket closed."); tracef("register test 3 finished.\n"); }
void regTest5() { tracef("reg test 5 begin:reg twice"); Context::Ptr pContext = new Context(Context::TLSV1_CLIENT_USE, "", Context::VERIFY_NONE); SecureStreamSocket sss(pContext); SocketAddress sa("127.0.0.1", 12222); sss.connect(sa, Timespan(3, 0)); DynamicStruct ds; ds["type"] = "request"; ds["action"] = "server.token"; Timestamp t; UInt64 tms = t.epochMicroseconds(); char tms_str[32]; snprintf(tms_str, 31, "%llu", tms); std::string key = "alpha2015"; key += tms_str; MD5Engine md5; md5.update(key); const DigestEngine::Digest& digest = md5.digest(); std::string md5key = DigestEngine::digestToHex(digest); DynamicStruct param; param["key"] = md5key; param["timestamp"] = tms_str; param["dev_name"] = "lock3"; param["dev_type"] = "sc-01"; param["uuid"] = "SC00000003"; ds["param"] = param; tracef("data send: %s.", ds.toString().c_str()); sss.sendBytes(ds.toString().c_str(), ds.toString().length()); char buf[1024] = {0, }; sss.receiveBytes(buf, 1024); JSON::Parser parser; Dynamic::Var var = parser.parse(buf); JSON::Object::Ptr pObj = var.extract<JSON::Object::Ptr>(); DynamicStruct ds_recv = *pObj; std::string token = ds_recv["param"]["token"]; DynamicStruct ds_reg; ds_reg["type"] = "request"; ds_reg["action"] = "server.register"; DynamicStruct param_reg; param_reg["token"] = token; param_reg["uuid"] = "SC00000003"; ds_reg["param"] = param_reg; StreamSocket ss; SocketAddress sa2("127.0.0.1", 13333); ss.connect(sa2, Timespan(3, 0)); ss.sendBytes(ds_reg.toString().c_str(), ds_reg.toString().length()); tracef("reg data send: %s.", ds_reg.toString().c_str()); memset(buf, 0, 1024); ss.receiveBytes(buf, 1024); tracef("receive bytes: %s.", buf); ss.close(); Thread::sleep(2000); StreamSocket ss2; ss2.connect(sa2, Timespan(3, 0)); tracef("reg data send again: %s.", ds_reg.toString().c_str()); ss2.sendBytes(ds_reg.toString().c_str(), ds_reg.toString().length()); memset(buf, 0, 1024); ss2.receiveBytes(buf, 1024); tracef("recv data: %s.", buf); ss2.close(); tracef("reg test 5 finished."); }
void CHTTPRequestHandler::handleRequest(HTTPServerRequest& request, HTTPServerResponse& response) { response.setContentType("application/json"); response.setChunkedTransferEncoding(true); if(m_buf == NULL) m_buf = new char[512]; memset(m_buf, 0, 512); request.stream().getline(m_buf, 512, '\n'); infof("%s, %d: Receive HTTP request[%s]", __FILE__, __LINE__, m_buf); JSON::Parser parser; Dynamic::Var var; try { var = parser.parse(m_buf); } catch(Exception& e) { JSON::Object::Ptr re = new JSON::Object; re->set(KEY_TYPE_STR, TYPE_RESPONSE_STR); re->set(KEY_RESULT_STR, RESULT_FAIL_STR); re->set(KEY_DETAIL_STR, "100"); DynamicStruct ds_res = *re; response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length()); infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str()); re = NULL; return; } JSON::Object::Ptr obj = var.extract<JSON::Object::Ptr>(); JSON::Object::Ptr res = new JSON::Object(*obj); if(!checkRequestFormat(obj, res)) { DynamicStruct ds_res = *res; response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length()); infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str()); res = NULL; return; } DynamicStruct ds = *obj; std::string uuid = ds[KEY_PARAM_STR][REG_UUID_STR].toString(); std::string action = ds[KEY_ACTION_STR].toString(); std::string component = ""; std::string method = ""; bool ret = parseAction(action, component, method); if(!ret) { res->set(KEY_RESULT_STR, RESULT_FAIL_STR); res->set(KEY_DETAIL_STR, "105"); DynamicStruct ds_res = *res; response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length()); infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str()); res = NULL; return; } if(component == COMPONENT_SERVER_STR) { if(method == SERVER_METHOD_CHECK) { res->set(KEY_RESULT_STR, RESULT_GOOD_STR); CDeviceManager* dev_mgr = CDeviceManager::instance(); DeviceInfo* dev_info = dev_mgr->getDevice(uuid); DynamicStruct param; param[REG_UUID_STR] = uuid; if(dev_info != NULL) { param[REG_STATE_STR] = "online"; param[REG_DEV_TYPE_STR] = dev_info->devType; } else { param[REG_STATE_STR] = "offline"; } res->remove(KEY_PARAM_STR); res->set(KEY_PARAM_STR, param); } else { res->set(KEY_RESULT_STR, RESULT_FAIL_STR); res->set(KEY_DETAIL_STR, "106"); } DynamicStruct ds_res = *res; infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str()); response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length()); res = NULL; return; } else if(component == COMPONENT_UPDATE_STR) { std::string detail = ""; if(method == UPDATE_METHOD_CHECK) { CUpdateManager* update_manager = CUpdateManager::instance(); JSON::Object::Ptr pParam = obj->getObject(KEY_PARAM_STR); if(update_manager->checkUpdate(pParam, detail)) { res->set(KEY_RESULT_STR, RESULT_GOOD_STR); res->remove(KEY_PARAM_STR); res->set(KEY_PARAM_STR, pParam); } else { res->set(KEY_RESULT_STR, RESULT_FAIL_STR); res->set(KEY_DETAIL_STR, detail); } } else { res->set(KEY_RESULT_STR, RESULT_FAIL_STR); res->set(KEY_DETAIL_STR, "106"); } DynamicStruct ds_res = *res; infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, ds_res.toString().c_str()); response.sendBuffer(ds_res.toString().c_str(), ds_res.toString().length()); res = NULL; return; } RequestInfo* req = new RequestInfo((UInt64)this, uuid, 5*1000*1000, obj); CRegServer* reg_server = CRegServer::instance(); //it will hang until response send back, or timeout reg_server->sendRequest(req); res = req->response; if(res.isNull()) { warnf("%s, %d: Request timeout.", __FILE__, __LINE__); DynamicStruct result = *obj; result[KEY_RESULT_STR] = RESULT_FAIL_STR; result[KEY_DETAIL_STR] = "timeout"; infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, result.toString().c_str()); response.sendBuffer(result.toString().c_str(), result.toString().length()); } else { DynamicStruct result = *res; infof("%s, %d: Send Http response[%s].", __FILE__, __LINE__, result.toString().c_str()); response.sendBuffer(result.toString().c_str(), result.toString().length()); } res = NULL; delete req; }