static void delete_secrets (NMSecretAgent *agent, NMConnection *connection, const char *connection_path, NMSecretAgentDeleteSecretsFunc callback, gpointer callback_data) { AppletAgentPrivate *priv = APPLET_AGENT_GET_PRIVATE (agent); Request *r; NMSettingConnection *s_con; const char *uuid; r = request_new (agent, connection, connection_path, NULL, NULL, FALSE, NULL, NULL, callback, callback_data); g_hash_table_insert (priv->requests, GUINT_TO_POINTER (r->id), r); s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); uuid = nm_setting_connection_get_uuid (s_con); g_assert (uuid); secret_password_clear (&network_manager_secret_schema, r->cancellable, delete_find_items_cb, r, KEYRING_UUID_TAG, uuid, NULL); r->keyring_calls++; }
static command_status_t fetch_domains(domain_set_t *ds, bool force, error_t **error) { if (!ds->uptodate || force) { request_t *req; bool request_success; json_document_t *doc; req = request_new(REQUEST_FLAG_SIGN, HTTP_GET, NULL, error, API_BASE_URL "/domain"); request_success = request_execute(req, RESPONSE_JSON, (void **) &doc, error); request_destroy(req); if (request_success) { Iterator it; json_value_t root; root = json_document_get_root(doc); hashtable_clear(ds->domains); json_array_to_iterator(&it, root); for (iterator_first(&it); iterator_is_valid(&it); iterator_next(&it)) { json_value_t v; v = (json_value_t) iterator_current(&it, NULL); hashtable_put(ds->domains, 0, json_get_string(v), domain_new(), NULL); // ds->domains has strdup as key_duper, don't need to strdup it ourself } iterator_close(&it); ds->uptodate = TRUE; json_document_destroy(doc); } else { return COMMAND_FAILURE; } } return COMMAND_SUCCESS; }
static command_status_t dnssec_status(COMMAND_ARGS) { request_t *req; bool request_success; json_document_t *doc; domain_record_argument_t *args; USED(mainopts); args = (domain_record_argument_t *) arg; assert(NULL != args->domain); req = request_new(REQUEST_FLAG_SIGN, HTTP_GET, NULL, error, API_BASE_URL "/domain/zone/%s/dnssec", args->domain); request_success = request_execute(req, RESPONSE_JSON, (void **) &doc, error); request_destroy(req); if (request_success) { json_value_t root, v; root = json_document_get_root(doc); #if 0 // for translations, do not remove _("enabled") _("disabled") _("enableInProgress") _("disableInProgress") #endif json_object_get_property(root, "status", &v); puts(_(json_get_string(v))); json_document_destroy(doc); } return request_success ? COMMAND_SUCCESS : COMMAND_FAILURE; }
static void delete_secrets (NMSecretAgent *agent, NMConnection *connection, const char *connection_path, NMSecretAgentDeleteSecretsFunc callback, gpointer callback_data) { AppletAgentPrivate *priv = APPLET_AGENT_GET_PRIVATE (agent); Request *r; NMSettingConnection *s_con; const char *uuid; KeyringCall *call; r = request_new (agent, connection, connection_path, NULL, NULL, FALSE, NULL, NULL, callback, callback_data); g_hash_table_insert (priv->requests, GUINT_TO_POINTER (r->id), r); s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); uuid = nm_setting_connection_get_uuid (s_con); g_assert (uuid); call = keyring_call_new (r); call->keyring_id = gnome_keyring_find_itemsv (GNOME_KEYRING_ITEM_GENERIC_SECRET, delete_find_items_cb, call, keyring_call_free, KEYRING_UUID_TAG, GNOME_KEYRING_ATTRIBUTE_TYPE_STRING, uuid, NULL); r->keyring_calls = g_slist_append (r->keyring_calls, call); }
static int process_message(SoupMessage *msg) { struct request *req; char *data; GTimer *timer; gulong microsecs; timer = g_timer_new(); g_timer_start(timer); req = request_new(msg->request_body->data); if (!req) return 1; data = request_response(req); if (!data) return 1; request_destroy(req); soup_message_set_response(msg, "application/json", SOUP_MEMORY_TAKE, data, strlen(data)); soup_message_set_status(msg, SOUP_STATUS_OK); g_timer_stop(timer); g_print("Time elapsed: %f\n", g_timer_elapsed(timer, µsecs)); g_timer_destroy(timer); return 0; }
void request_send(struct fuse_conn *fc, struct fuse_in *in, struct fuse_out *out) { struct fuse_req *req; out->h.error = -ERESTARTSYS; if(down_interruptible(&fc->outstanding)) return; out->h.error = -ENOMEM; req = request_new(); if(!req) return; req->in = in; req->out = out; req->issync = 1; spin_lock(&fuse_lock); out->h.error = -ENOTCONN; if(fc->file) { in->h.unique = get_unique(fc); list_add_tail(&req->list, &fc->pending); wake_up(&fc->waitq); request_wait_answer(req); list_del(&req->list); } spin_unlock(&fuse_lock); request_free(req); up(&fc->outstanding); }
static gpointer agent_new_save_delete (NMSecretAgent *self, NMConnection *connection, NMConnectionSerializationFlags flags, const char *method, NMSecretAgentCallback callback, gpointer callback_data) { NMSecretAgentPrivate *priv = NM_SECRET_AGENT_GET_PRIVATE (self); GVariant *dict; GHashTable *hash; Request *r; const char *cpath = nm_connection_get_path (connection); dict = nm_connection_to_dbus (connection, flags); hash = nm_utils_connection_dict_to_hash (dict); g_variant_unref (dict); r = request_new (self, cpath, NULL, callback, callback_data); r->call = dbus_g_proxy_begin_call_with_timeout (priv->proxy, method, agent_save_delete_cb, r, NULL, 10000, /* 10 seconds */ DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, hash, DBUS_TYPE_G_OBJECT_PATH, cpath, G_TYPE_INVALID); g_hash_table_insert (priv->requests, r->call, r); g_hash_table_destroy (hash); return r->call; }
static command_status_t domain_export(COMMAND_ARGS) { xmlDocPtr doc; request_t *req; xmlNodePtr root; bool request_success; domain_record_argument_t *args; USED(mainopts); args = (domain_record_argument_t *) arg; assert(NULL != args->domain); req = request_new(REQUEST_FLAG_SIGN, HTTP_GET, NULL, error, API_BASE_URL "/domain/zone/%s/export", args->domain); REQUEST_XML_RESPONSE_WANTED(req); // we ask XML instead of JSON else we have to parse invalid json document or to unescape characters if ((request_success = request_execute(req, RESPONSE_XML, (void **) &doc, error))) { if (NULL != (root = xmlDocGetRootElement(doc))) { xmlChar *content; content = xmlNodeGetContent(root); puts((const char *) content); xmlFree(content); } request_success = NULL != root; xmlFreeDoc(doc); } request_destroy(req); return request_success ? COMMAND_SUCCESS : COMMAND_FAILURE; }
void mwServiceStorage_save(struct mwServiceStorage *srvc, struct mwStorageUnit *item, mwStorageCallback cb, gpointer data, GDestroyNotify d_free) { /* - construct a request - put request at end of pending - if channel is open and connected - compose the save message - send message - set request to sent - else - start service */ struct mwStorageReq *req; req = request_new(srvc, item, cb, data, d_free); req->action = action_save; srvc->pending = g_list_append(srvc->pending, req); if(MW_SERVICE_IS_STARTED(MW_SERVICE(srvc))) request_send(srvc->channel, req); }
static bool get_domain_records(const char *domain, domain_t **d, bool force, error_t **error) { domain_set_t *ds; bool request_success; *d = NULL; FETCH_ACCOUNT_DOMAINS(ds); request_success = TRUE; // TODO: hashtable_clear((*d)->records) if force if (!hashtable_get(ds->domains, domain, d) || !(*d)->uptodate || force) { request_t *req; json_document_t *doc; if (NULL == *d) { *d = domain_new(); hashtable_put(ds->domains, 0, domain, *d, NULL); } req = request_new(REQUEST_FLAG_SIGN, HTTP_GET, NULL, error, API_BASE_URL "/domain/zone/%s/record", domain); request_success = request_execute(req, RESPONSE_JSON, (void **) &doc, error); request_destroy(req); // result if (request_success) { Iterator it; json_value_t root; root = json_document_get_root(doc); json_array_to_iterator(&it, root); for (iterator_first(&it); request_success && iterator_is_valid(&it); iterator_next(&it)) { json_value_t v; json_document_t *doc; v = (json_value_t) iterator_current(&it, NULL); req = request_new(REQUEST_FLAG_SIGN, HTTP_GET, NULL, error, API_BASE_URL "/domain/zone/%s/record/%u", domain, json_get_integer(v)); request_success = request_execute(req, RESPONSE_JSON, (void **) &doc, error); request_destroy(req); // result parse_record((*d)->records, doc); } iterator_close(&it); json_document_destroy(doc); (*d)->uptodate = TRUE; } } return request_success ? COMMAND_SUCCESS : COMMAND_FAILURE; }
void respond(request **req, int client_socket) { response *resp = make_response(*req); response_write(resp, client_socket); response_clear(resp); request_clear(*req); *req = request_new(); }
// ./ovh domain domain.ext record toto add www type CNAME // TODO: ttl (optionnal) static command_status_t record_add(COMMAND_ARGS) { bool request_success; json_document_t *doc; domain_record_argument_t *args; USED(mainopts); args = (domain_record_argument_t *) arg; assert(NULL != args->domain); assert(NULL != args->record); { json_document_t *reqdoc; // data { json_value_t root; reqdoc = json_document_new(); root = json_object(); json_object_set_property(root, "target", json_string(args->value)); json_object_set_property(root, "fieldType", json_string(domain_record_types[args->type])); // if ('\0' != *subdomain) json_object_set_property(root, "subDomain", json_string(args->record)); json_document_set_root(reqdoc, root); } // request { request_t *req; req = request_new(REQUEST_FLAG_SIGN | REQUEST_FLAG_JSON, HTTP_POST, reqdoc, error, API_BASE_URL "/domain/zone/%s/record", args->domain); request_success = request_execute(req, RESPONSE_JSON, (void **) &doc, error); request_destroy(req); json_document_destroy(reqdoc); } } // result if (request_success) { domain_t *d; ht_hash_t h; domain_set_t *ds; d = NULL; ds = NULL; account_current_get_data(MODULE_NAME, (void **) &ds); assert(NULL != ds); h = hashtable_hash(ds->domains, args->domain); if (!hashtable_quick_get(ds->domains, h, args->domain, &d)) { d = domain_new(); hashtable_quick_put(ds->domains, 0, h, args->domain, d, NULL); } parse_record(d->records, doc); ask_for_refresh(RELAY_COMMAND_ARGS); } return request_success ? COMMAND_SUCCESS : COMMAND_FAILURE; }
static command_status_t dnssec_enable_disable(COMMAND_ARGS) { request_t *req; bool request_success; domain_record_argument_t *args; USED(mainopts); args = (domain_record_argument_t *) arg; assert(NULL != args->domain); if (args->on_off) { req = request_new(REQUEST_FLAG_SIGN, HTTP_POST, NULL, error, API_BASE_URL "/domain/zone/%s/dnssec", args->domain); } else { req = request_new(REQUEST_FLAG_SIGN, HTTP_DELETE, NULL, error, API_BASE_URL "/domain/zone/%s/dnssec", args->domain); } request_success = request_execute(req, RESPONSE_IGNORE, NULL, error); request_destroy(req); return request_success ? COMMAND_SUCCESS : COMMAND_FAILURE; }
static command_status_t record_delete(COMMAND_ARGS) { domain_t *d; record_t *match; bool request_success; domain_record_argument_t *args; args = (domain_record_argument_t *) arg; assert(NULL != args->domain); assert(NULL != args->record); if ((request_success = (COMMAND_SUCCESS == get_domain_records(args->domain, &d, FALSE, error)))) { #if 0 // what was the goal? If true, it is more the domain which does not exist! if (!hashtable_get(domains, argv[0], &d)) { error_set(error, WARN, "Domain '%s' doesn't have any record named '%s'\n", argv[0], argv[3]); return COMMAND_FAILURE; } #endif { size_t matches; matches = find_record(d->records, args->record, &match); switch (matches) { case 1: { if (!confirm(mainopts, _("Confirm deletion of '%s.%s'"), match->name, args->domain)) { return COMMAND_SUCCESS; // yeah, success because user canceled it } break; } case 0: error_set(error, INFO, "No record match '%s'", args->record); return COMMAND_SUCCESS; default: error_set(error, WARN, "Abort, more than one record match '%s'", args->record); return COMMAND_FAILURE; } } { request_t *req; // request req = request_new(REQUEST_FLAG_SIGN, HTTP_DELETE, NULL, error, API_BASE_URL "/domain/zone/%s/record/%" PRIu32, args->domain, match->id); request_success = request_execute(req, RESPONSE_IGNORE, NULL, error); request_destroy(req); // result if (request_success) { hashtable_quick_delete(d->records, match->id, NULL, TRUE); ask_for_refresh(RELAY_COMMAND_ARGS); } } } return request_success ? COMMAND_SUCCESS : COMMAND_FAILURE; }
/* * Get version number */ static gint get_version(QQInfo *info) { int ret = NO_ERR; Request *req = request_new(); Response *rps = NULL; gint res = 0; request_set_method(req, "GET"); request_set_version(req, "HTTP/1.1"); request_set_uri(req, VERPATH); request_set_default_headers(req); request_add_header(req, "Host", LOGINPAGEHOST); Connection *con = connect_to_host(LOGINPAGEHOST, 80); if(con == NULL){ g_warning("Can NOT connect to server!(%s, %d)" , __FILE__, __LINE__); request_del(req); return NETWORK_ERR; } send_request(con, req); res = rcv_response(con, &rps); close_con(con); connection_free(con); const gchar *retstatus = rps -> status -> str; if (-1 == res || !rps) { g_warning("Null point access (%s, %d)\n", __FILE__, __LINE__); ret = -1; goto error; } if(g_strstr_len(retstatus, -1, "200") == NULL){ g_warning("Server status %s (%s, %d)", retstatus , __FILE__, __LINE__); ret = NETWORK_ERR; goto error; } gchar *lb, *rb; gchar *ms = rps -> msg -> str; lb = g_strstr_len(ms, -1, "("); if(lb == NULL){ g_warning("Get version error!! %s (%s, %d)",rps -> msg -> str , __FILE__, __LINE__); ret = NETWORK_ERR; goto error; } ++lb; rb = g_strstr_len(ms, -1, ")"); *rb = '\0'; info -> version = g_string_new(lb); g_debug("Version: %s(%s, %d)", lb, __FILE__, __LINE__); error: request_del(req); response_del(rps); return ret; }
static void http_request(struct conn *conn) { printf("--> request\n"); if (!conn->request) conn->request = request_new(); parse_request(conn->request, conn->io); conn->service->cb(conn->service, conn); }
static command_status_t domain_check(COMMAND_ARGS) { bool success; domain_set_t *ds; USED(arg); USED(mainopts); FETCH_ACCOUNT_DOMAINS(ds); // populate if ((success = (COMMAND_SUCCESS == fetch_domains(ds, FALSE, error)))) { time_t now; Iterator it; now = time(NULL); hashtable_to_iterator(&it, ds->domains); for (iterator_first(&it); success && iterator_is_valid(&it); iterator_next(&it)) { request_t *req; json_document_t *doc; const char *domain_name; iterator_current(&it, (void **) &domain_name); // request req = request_new(REQUEST_FLAG_SIGN, HTTP_GET, NULL, error, API_BASE_URL "/domain/%s/serviceInfos", domain_name); success = request_execute(req, RESPONSE_JSON, (void **) &doc, error); request_destroy(req); // response if (success) { time_t domain_expiration; json_value_t root, expiration; root = json_document_get_root(doc); if (json_object_get_property(root, "expiration", &expiration)) { if (date_parse_to_timestamp(json_get_string(expiration), NULL, &domain_expiration)) { int diff_days; diff_days = date_diff_in_days(domain_expiration, now); if (diff_days > 0 && diff_days < 3000) { printf("%s expires in %d days\n", domain_name, diff_days); } } } json_document_destroy(doc); } } iterator_close(&it); } return success ? COMMAND_SUCCESS : COMMAND_FAILURE; }
static void save_secrets (NMSecretAgent *agent, NMConnection *connection, const char *connection_path, NMSecretAgentSaveSecretsFunc callback, gpointer callback_data) { AppletAgentPrivate *priv = APPLET_AGENT_GET_PRIVATE (agent); Request *r; r = request_new (agent, connection, connection_path, NULL, NULL, FALSE, NULL, callback, NULL, callback_data); g_hash_table_insert (priv->requests, GUINT_TO_POINTER (r->id), r); /* First delete any existing items in the keyring */ nm_secret_agent_delete_secrets (agent, connection, save_delete_cb, r); }
static command_status_t domain_refresh(COMMAND_ARGS) { request_t *req; bool request_success; domain_record_argument_t *args; USED(mainopts); args = (domain_record_argument_t *) arg; assert(NULL != args->domain); req = request_new(REQUEST_FLAG_SIGN, HTTP_POST, NULL, error, API_BASE_URL "/domain/zone/%s/refresh", args->domain); request_success = request_execute(req, RESPONSE_IGNORE, NULL, error); // Response is void request_destroy(req); return request_success ? COMMAND_SUCCESS : COMMAND_FAILURE; }
gconstpointer nm_secret_agent_get_secrets (NMSecretAgent *self, NMConnection *connection, const char *setting_name, const char **hints, NMSecretAgentGetSecretsFlags flags, NMSecretAgentCallback callback, gpointer callback_data) { NMSecretAgentPrivate *priv; GVariant *dict; GHashTable *hash; Request *r; g_return_val_if_fail (self != NULL, NULL); g_return_val_if_fail (connection != NULL, NULL); g_return_val_if_fail (setting_name != NULL, NULL); priv = NM_SECRET_AGENT_GET_PRIVATE (self); g_return_val_if_fail (priv->proxy != NULL, NULL); dict = nm_connection_to_dbus (connection, NM_CONNECTION_SERIALIZE_ALL); hash = nm_utils_connection_dict_to_hash (dict); g_variant_unref (dict); /* Mask off the private flags if present */ flags &= ~NM_SECRET_AGENT_GET_SECRETS_FLAG_ONLY_SYSTEM; flags &= ~NM_SECRET_AGENT_GET_SECRETS_FLAG_NO_ERRORS; r = request_new (self, nm_connection_get_path (connection), setting_name, callback, callback_data); r->call = dbus_g_proxy_begin_call_with_timeout (priv->proxy, "GetSecrets", get_callback, r, NULL, 120000, /* 120 seconds */ DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, hash, DBUS_TYPE_G_OBJECT_PATH, nm_connection_get_path (connection), G_TYPE_STRING, setting_name, G_TYPE_STRV, hints, G_TYPE_UINT, flags, G_TYPE_INVALID); g_hash_table_insert (priv->requests, r->call, r); g_hash_table_destroy (hash); return r->call; }
void accept_handler(aeEventLoop *el, int fd, void *privdata, int mask) { int cport, cfd; char cip[128]; (void) el; (void)privdata; (void)mask; struct request *req; req = request_new(); cfd = anetTcpAccept(_svr.neterr,fd,cip,&cport); if (cfd == AE_ERR) return; _clicount++; aeCreateFileEvent(_svr.el,cfd,AE_READABLE,read_handler,req); }
int main(int argc, char** argv) { Request* r = request_new(); request_set_method(r, "GET"); request_set_uri(r, "http://web.qq.com/hello.html?key=234"); request_set_version(r, "HTTP/1.1"); request_add_header(r, "Content-Lenght", "12345"); request_add_header(r, "Host", "web.qq.com"); gchar* s = "asdjfaslkdjfoqwiuertjakfdja;sldf"; request_append_msg(r, s, strlen(s)); request_append_msg(r, s, strlen(s)); request_append_msg(r, s, strlen(s)); request_append_msg(r, s, strlen(s)); request_append_msg(r, s, strlen(s)); request_append_msg(r, s, strlen(s)); print_raw(request_tostring(r)); g_printf("\n\n"); s = "HTTP/1.1 200 OK\r\n" "Date: Sun, 02 Jan 2011 19 19:09:55 GMT\r\n" "Server: Tencent Login Server/2.0.0\r\n" "Set-Cookie: pt2gguin=o0494498045; EXPIRES=Fri, 02-Jan-2020 00:00:00 GMT; PATH=/;DOMAIN=qq.com;\r\n" "Set-Cookie: uin=o0494498045; PATH=/; DOMAIN=qq.com;\r\n" "Set-Cookie: skey=@EK66ZxUe8; PATH=/; DOMAIN=qq.com;\r\n" "Set-Cookie: clientuin=; EXPIRES=Fri, 02-Jan-1970 00:00:00 GMT; PATH=/; DOMAIN=qq.com;\r\n" "Set-Cookie: clientkey=; EXPIRES=Fri, 02-Jan-1970 00:00:00 GMT; PATH=/; DOMAIN=qq.com;\r\n" "Set-Cookie: zzpaneluin=; EXPIRES=Fri, 02-Jan-1970 00:00:00 GMT; PATH=/; DOMAIN=qq.com;\r\n" "Set-Cookie: zzpanelkey=; EXPIRES=Fri, 02-Jan-1970 00:00:00 GMT; PATH=/; DOMAIN=qq.com;\r\n" "Set-Cookie: ptisp=edu;PATH=/; DOMAIN=qq.com;\r\n" "Set-Cookie:ptuserinfo=4b65726e656c;PATH=/;DOMAIN=ptlogin2.qq.com;\r\n" "Set-Cookie: ptwebqq=b16f2ca37c56d8a2ce064a9494cc85044e78ac6c480041c3f5be450206ab6728;" "PATH=/; DOMAIN=qq.com;\r\n" "Pragma: no-cache\r\n" "Cache-Control: no-cache; must-revalidate\r\n" "Connection: Close\r\n" "Content-Type: application/x-javascript; charset=utf-8\r\n"; Response* rps = response_new_parse(g_string_new(s)); g_printf("%s\n",response_tostring(rps) -> str); g_printf("\n\n"); print_raw(rps -> reason); print_raw(rps -> msg); return 0; }
gconstpointer nm_secret_agent_get_secrets (NMSecretAgent *self, NMConnection *connection, const char *setting_name, const char *hint, NMSettingsGetSecretsFlags flags, NMSecretAgentCallback callback, gpointer callback_data) { NMSecretAgentPrivate *priv; GHashTable *hash; const char *hints[2] = { hint, NULL }; Request *r; g_return_val_if_fail (self != NULL, NULL); g_return_val_if_fail (connection != NULL, NULL); g_return_val_if_fail (setting_name != NULL, NULL); priv = NM_SECRET_AGENT_GET_PRIVATE (self); hash = nm_connection_to_hash (connection, NM_SETTING_HASH_FLAG_ALL); /* Mask off the private ONLY_SYSTEM flag if present */ flags &= ~NM_SETTINGS_GET_SECRETS_FLAG_ONLY_SYSTEM; r = request_new (self, nm_connection_get_path (connection), setting_name, callback, callback_data); r->call = dbus_g_proxy_begin_call_with_timeout (priv->proxy, "GetSecrets", get_callback, r, NULL, 120000, /* 120 seconds */ DBUS_TYPE_G_MAP_OF_MAP_OF_VARIANT, hash, DBUS_TYPE_G_OBJECT_PATH, nm_connection_get_path (connection), G_TYPE_STRING, setting_name, G_TYPE_STRV, hints, G_TYPE_UINT, flags, G_TYPE_INVALID); g_hash_table_insert (priv->requests, r->call, r); g_hash_table_destroy (hash); return r->call; }
/* This one is currently only used for sending FORGET and RELEASE, which are kernel initiated request. So the outstanding semaphore is not used. */ int request_send_noreply(struct fuse_conn *fc, struct fuse_in *in) { struct fuse_req *req; req = request_new(); if(!req) return -ENOMEM; req->in = in; req->issync = 0; spin_lock(&fuse_lock); if(!fc->file) { spin_unlock(&fuse_lock); request_free(req); return -ENOTCONN; } list_add_tail(&req->list, &fc->pending); wake_up(&fc->waitq); spin_unlock(&fuse_lock); return 0; }
/* TODO: factor */ static void * worker_thread (void *data) { worker_closure_t *closure = (worker_closure_t *) data; mock_server_t *server = closure->server; mongoc_stream_t *client_stream = closure->client_stream; mongoc_buffer_t buffer; mongoc_rpc_t *rpc = NULL; bool handled; bson_error_t error; int32_t msg_len; bool stopped; sync_queue_t *q; request_t *request; mongoc_array_t autoresponders; ssize_t i; autoresponder_handle_t handle; #ifdef MONGOC_ENABLE_SSL bool ssl; #endif ENTRY; BSON_ASSERT(closure); #ifdef MONGOC_ENABLE_SSL mongoc_mutex_lock (&server->mutex); ssl = server->ssl; mongoc_mutex_unlock (&server->mutex); if (ssl) { if (!mongoc_stream_tls_handshake_block (client_stream, "localhost", TIMEOUT, &error)) { MONGOC_ERROR("Blocking TLS handshake failed"); mongoc_stream_close (client_stream); mongoc_stream_destroy (client_stream); RETURN (NULL); } } #endif _mongoc_buffer_init (&buffer, NULL, 0, NULL, NULL); _mongoc_array_init (&autoresponders, sizeof (autoresponder_handle_t)); again: bson_free (rpc); rpc = NULL; handled = false; mongoc_mutex_lock (&server->mutex); stopped = server->stopped; mongoc_mutex_unlock (&server->mutex); if (stopped) { GOTO(failure); } if (_mongoc_buffer_fill (&buffer, client_stream, 4, TIMEOUT, &error) == -1) { GOTO (again); } assert (buffer.len >= 4); memcpy (&msg_len, buffer.data + buffer.off, 4); msg_len = BSON_UINT32_FROM_LE (msg_len); if (msg_len < 16) { MONGOC_WARNING ("No data"); GOTO (failure); } if (_mongoc_buffer_fill (&buffer, client_stream, (size_t) msg_len, -1, &error) == -1) { MONGOC_WARNING ("%s():%d: %s", BSON_FUNC, __LINE__, error.message); GOTO (failure); } assert (buffer.len >= (unsigned) msg_len); /* copies message from buffer */ request = request_new (&buffer, msg_len, server, client_stream, closure->port); mongoc_mutex_lock (&server->mutex); _mongoc_array_copy (&autoresponders, &server->autoresponders); mongoc_mutex_unlock (&server->mutex); if (mock_server_get_verbose (server)) { printf ("%5.2f %hu -> %hu %s\n", mock_server_get_uptime_sec (server), closure->port, server->port, request->as_str); fflush (stdout); } /* run responders most-recently-added-first */ for (i = server->autoresponders.len - 1; i >= 0; i--) { handle = _mongoc_array_index (&server->autoresponders, autoresponder_handle_t, i); if (handle.responder (request, handle.data)) { handled = true; /* responder should destroy the request */ request = NULL; break; } } if (!handled) { q = mock_server_get_queue (server); q_put (q, (void *) request); request = NULL; } memmove (buffer.data, buffer.data + buffer.off + msg_len, buffer.len - msg_len); buffer.off = 0; buffer.len -= msg_len; GOTO (again); failure: _mongoc_array_destroy (&autoresponders); _mongoc_buffer_destroy (&buffer); mongoc_stream_close (client_stream); mongoc_stream_destroy (client_stream); bson_free (rpc); bson_free (closure); _mongoc_buffer_destroy (&buffer); RETURN (NULL); }
static gint do_send_buddy_msg(QQInfo *info, QQSendMsg *msg, GError **err) { GString *uin = msg -> to_uin; gint ret_code = 0; gint res = 0; gchar params[3000]; g_debug("Send msg to %s!(%s, %d)", uin -> str, __FILE__, __LINE__); Request *req = request_new(); Response *rps = NULL; request_set_method(req, "POST"); request_set_version(req, "HTTP/1.1"); request_set_uri(req, MSGFRIPATH); request_set_default_headers(req); request_add_header(req, "Host", MSGHOST); request_add_header(req, "Cookie", info -> cookie -> str); request_add_header(req, "Origin", "http://d.web2.qq.com"); request_add_header(req, "Content-Type", "application/x-www-form-urlencoded"); request_add_header(req, "Referer" , "http://"MSGHOST"/proxy.html?v=20110331002&callback=2"); GString *content; content = qq_sendmsg_contents_tostring(msg); g_snprintf(params, 3000, "r={\"to\":%s,\"face\":%s," "%s,\"msg_id\":%s," "\"clientid\":\"%s\",\"psessionid\":\"%s\"}" , uin -> str, msg -> face -> str , content -> str , msg -> msg_id -> str , info -> clientid -> str , info -> psessionid -> str); g_string_free(content, TRUE); gchar *euri = g_uri_escape_string(params, "=", FALSE); request_append_msg(req, euri, strlen(euri)); g_free(euri); g_snprintf(params, 3000, "%u", (unsigned int)strlen(req -> msg -> str)); request_add_header(req, "Content-Length", params); Connection *con = connect_to_host(MSGHOST, 80); if(con == NULL){ g_warning("Can NOT connect to server!(%s, %d)" , __FILE__, __LINE__); request_del(req); return -1; } send_request(con, req); res = rcv_response(con, &rps); close_con(con); connection_free(con); if (-1 == res || !rps) { g_warning("Null point access (%s, %d)\n", __FILE__, __LINE__); ret_code = -1; goto error; } const gchar *retstatus = rps -> status -> str; json_t *json = NULL; if(g_strstr_len(retstatus, -1, "200") == NULL){ /* * Maybe some error occured. */ g_warning("Resoponse status is NOT 200, but %s (%s, %d)" , retstatus, __FILE__, __LINE__); ret_code = -1; goto error; } switch(json_parse_document(&json, rps -> msg -> str)) { case JSON_OK: break; default: g_warning("json_parser_document: syntax error. (%s, %d)" , __FILE__, __LINE__); } json_t *val = json_find_first_label_all(json, "result"); if(val != NULL){ val = val -> child; if(g_strstr_len(val -> text, -1, "ok") == NULL){ g_warning("Server return error. %s (%s, %d)" , val -> text, __FILE__, __LINE__); } }else{ g_warning("Server return: (%s, %d)%s", __FILE__, __LINE__ , rps -> msg -> str); } json_free_value(&json); error: request_del(req); response_del(rps); return ret_code; }
static gboolean handle_screenshot (XdpImplScreenshot *object, GDBusMethodInvocation *invocation, const char *arg_handle, const char *arg_app_id, const char *arg_parent_window, GVariant *arg_options) { g_autoptr(Request) request = NULL; const char *sender; ScreenshotDialogHandle *handle; g_autoptr(GError) error = NULL; g_autofree char *filename = NULL; gboolean success; gboolean modal; GtkWidget *dialog; GdkDisplay *display; GdkScreen *screen; ExternalWindow *external_parent = NULL; GtkWidget *fake_parent; sender = g_dbus_method_invocation_get_sender (invocation); request = request_new (sender, arg_app_id, arg_handle); org_gnome_shell_screenshot_call_screenshot_sync (shell, FALSE, TRUE, "Screenshot", &success, &filename, NULL, NULL); if (!g_variant_lookup (arg_options, "modal", "b", &modal)) modal = TRUE; if (arg_parent_window) { external_parent = create_external_window_from_handle (arg_parent_window); if (!external_parent) g_warning ("Failed to associate portal window with parent window %s", arg_parent_window); } if (external_parent) display = external_window_get_display (external_parent); else display = gdk_display_get_default (); screen = gdk_display_get_default_screen (display); fake_parent = g_object_new (GTK_TYPE_WINDOW, "type", GTK_WINDOW_TOPLEVEL, "screen", screen, NULL); g_object_ref_sink (fake_parent); dialog = GTK_WIDGET (screenshot_dialog_new (arg_app_id, filename)); gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (fake_parent)); gtk_window_set_modal (GTK_WINDOW (dialog), modal); handle = g_new0 (ScreenshotDialogHandle, 1); handle->impl = object; handle->invocation = invocation; handle->request = g_object_ref (request); handle->dialog = g_object_ref (dialog); handle->external_parent = external_parent; handle->uri = g_filename_to_uri (filename, NULL, NULL); g_signal_connect (request, "handle-close", G_CALLBACK (handle_close), handle); g_signal_connect (dialog, "done", G_CALLBACK (screenshot_dialog_done), handle); gtk_widget_realize (dialog); if (external_parent) external_window_set_parent_of (external_parent, gtk_widget_get_window (dialog)); gtk_widget_show (dialog); request_export (request, g_dbus_method_invocation_get_connection (invocation)); return TRUE; }
static void get_secrets (NMSecretAgent *agent, NMConnection *connection, const char *connection_path, const char *setting_name, const char **hints, guint32 flags, NMSecretAgentGetSecretsFunc callback, gpointer callback_data) { AppletAgentPrivate *priv = APPLET_AGENT_GET_PRIVATE (agent); Request *r; GError *error = NULL; NMSettingConnection *s_con; NMSetting *setting; const char *uuid, *ctype; GHashTable *attrs; setting = nm_connection_get_setting_by_name (connection, setting_name); if (!setting) { error = g_error_new (NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_INVALID_CONNECTION, "%s.%d - Connection didn't have requested setting '%s'.", __FILE__, __LINE__, setting_name); callback (agent, connection, NULL, error, callback_data); g_error_free (error); return; } uuid = nm_connection_get_uuid (connection); s_con = nm_connection_get_setting_connection (connection); g_assert (s_con); ctype = nm_setting_connection_get_connection_type (s_con); if (!uuid || !ctype) { error = g_error_new (NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_INVALID_CONNECTION, "%s.%d - Connection didn't have required UUID.", __FILE__, __LINE__); callback (agent, connection, NULL, error, callback_data); g_error_free (error); return; } /* Track the secrets request */ r = request_new (agent, connection, connection_path, setting_name, hints, flags, callback, NULL, NULL, callback_data); g_hash_table_insert (priv->requests, GUINT_TO_POINTER (r->id), r); /* VPN passwords are handled by the VPN plugin's auth dialog */ if (!strcmp (ctype, NM_SETTING_VPN_SETTING_NAME)) { ask_for_secrets (r); return; } /* Only handle non-VPN secrets if we're supposed to */ if (priv->vpn_only == TRUE) { error = g_error_new_literal (NM_SECRET_AGENT_ERROR, NM_SECRET_AGENT_ERROR_NO_SECRETS, "Only handling VPN secrets at this time."); callback (agent, connection, NULL, error, callback_data); g_error_free (error); return; } /* For everything else we scrape the keyring for secrets first, and ask * later if required. */ attrs = secret_attributes_build (&network_manager_secret_schema, KEYRING_UUID_TAG, uuid, KEYRING_SN_TAG, setting_name, NULL); secret_service_search (NULL, &network_manager_secret_schema, attrs, SECRET_SEARCH_ALL | SECRET_SEARCH_UNLOCK | SECRET_SEARCH_LOAD_SECRETS, r->cancellable, keyring_find_secrets_cb, r); r->keyring_calls++; g_hash_table_unref (attrs); }
// arguments: [ttl <int>] [name <string>] [target <string>] // (in any order) // if we change record's name (subDomain), we need to update records cache static command_status_t record_update(COMMAND_ARGS) { domain_t *d; record_t *r; bool success; domain_record_argument_t *args; USED(mainopts); args = (domain_record_argument_t *) arg; assert(NULL != args->domain); assert(NULL != args->record); if ((success = (COMMAND_SUCCESS == get_domain_records(args->domain, &d, FALSE, error)))) { json_document_t *reqdoc; size_t matches; matches = find_record(d->records, args->record, &r); switch (matches) { case 1: // NOP : OK break; case 0: error_set(error, WARN, "Abort, no record match '%s'", args->record); return COMMAND_FAILURE; default: error_set(error, WARN, "Abort, more than one record match '%s'", args->record); return COMMAND_FAILURE; } // data { json_value_t root; reqdoc = json_document_new(); root = json_object(); if (NULL != args->value) { json_object_set_property(root, "target", json_string(args->value)); } if (NULL != args->name) { json_object_set_property(root, "subDomain", json_string(args->name)); } json_object_set_property(root, "ttl", json_integer(args->ttl)); json_document_set_root(reqdoc, root); } // request { request_t *req; req = request_new(REQUEST_FLAG_SIGN | REQUEST_FLAG_JSON, HTTP_PUT, reqdoc, error, API_BASE_URL "/domain/zone/%s/record/%" PRIu32, args->domain, r->id); success = request_execute(req, RESPONSE_IGNORE, NULL, error); request_destroy(req); json_document_destroy(reqdoc); } if (success) { if (NULL != args->value) { FREE(r, target); r->target = strdup(args->value); } if (NULL != args->name) { FREE(r, name); r->name = strdup(args->name); } r->ttl = args->ttl; ask_for_refresh(RELAY_COMMAND_ARGS); } } // debug("request update of %s.%s to %s.%s with TTL = %" PRIu32 " and value = '%s'", args->record, args->domain, args->name, args->domain, args->ttl, args->value); return COMMAND_SUCCESS; }
int main(/*int argc, char *argv[]*/) { INFO("Started\n"); int server_socket, client_socket, port, res, client_len, n; struct sockaddr_in srv_addr, client_addr; int iSetOption = 1; port = SRVPORT; server_socket = socket(AF_INET, SOCK_STREAM, 0); setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (char*)&iSetOption, sizeof(iSetOption)); if (server_socket < 0) { ERROR("Error opening socket\n"); IFERROR(perror("")); } INFO("Made server socket\n"); memset((char *) &srv_addr, 0, sizeof(srv_addr)); srv_addr.sin_family = AF_INET; srv_addr.sin_port = htons(port); srv_addr.sin_addr.s_addr = INADDR_ANY; while ((res = bind(server_socket, (struct sockaddr *) &srv_addr, sizeof(srv_addr))) < 0) { ERROR("Error binding socket\n"); IFERROR(perror("")); } listen(server_socket, 5); INFO("Listening on port %d\n", port); client_len = sizeof(client_addr); if (chdir("content")) { ERROR("Could not chdir to the content directory\n"); IFERROR(perror("")); exit(1); } do { client_socket = accept(server_socket, (struct sockaddr *) &client_addr, (socklen_t *) &client_len); INFO("Accepted client socket\n"); if (client_socket < 0) { ERROR("Error on client accept\n"); IFERROR(perror("")); } request *req = request_new(); char line[MAXLEN]; while ((n = fd_readline(client_socket, line, MAXLEN)) > 0) { if (n == MAXLEN-1 && line[MAXLEN-1] != '\n') { WARN("Line length exceeded MAXLEN %d\n", MAXLEN); char c; int dropped = 0; while ((n = read(client_socket, &c, 1)) > 0) { dropped += 1; if (c == '\n') break; } WARN("Skipped to the next line, dropped %d characters\n", dropped); } if (is_crlf(line)) { // We've reached the end of the headers int left_to_read = request_get_content_length(req); int total_read = 0, len; if (left_to_read > 0) { DEBUG("There's %d chars of body to read\n", left_to_read); // There's a Content-Length so there must be a body req->body = malloc(sizeof(char) * (left_to_read+1)); len = MAXLEN < left_to_read ? MAXLEN : left_to_read; len += 1; while (left_to_read && (n = fd_readline(client_socket, line, len)) > 0) { DEBUG("Read %d characters of body: %s\n", n, line); strcpy(&req->body[total_read], line); total_read += n; left_to_read -= n; DEBUG("total_read: %d, left_to_read: %d\n", total_read, left_to_read); len = MAXLEN < left_to_read ? MAXLEN : left_to_read; } req->body[total_read+1] = '\0'; DEBUG("Done reading body in %d chars:\n", total_read); DEBUG("%s\n", req->body); respond(&req, client_socket); continue; } else { DEBUG("No body to read, generating response\n"); respond(&req, client_socket); continue; } } if (request_parse(req, line)) { WARN("An error occurred while parsing the previous line\n"); } } DEBUG("Done with this request! n = %d\n", n); } while (1); close(server_socket); close(client_socket); return 0; }