int bridge_request_handle(bridge_request_t *self)
{
	int ret;
	char *buffer;
	struct json_object *in_json;


	if ((ret = bridge_request_getinput(self, &buffer)) != 0) {
		bridge_request_fatal(self);
		goto out;
	}
	in_json = json_tokener_parse_ex(self->tokener, buffer, -1);
	if (!in_json) {
		bridge_request_fatal(self);
		ret = -1;
		goto cleanup;
	}

	ret = bridge_request_call_json_dbus(self, in_json);

cleanup:
	json_object_put(in_json);
	json_tokener_reset(self->tokener);
	free(buffer);
out:
	if (ret != 0)
		FCGX_Finish_r(&self->request);
	return ret;
}
Example #2
0
iot_json_t *iot_json_string_to_object(const char *s, int len)
{
    if (parser == NULL) {
        parser = json_tokener_new();

        if (parser == NULL)
            return NULL;
    }
    else
        json_tokener_reset(parser);

    if (len < 0)
        len = strlen(s);

    return json_tokener_parse_ex(parser, s, len);
}
Example #3
0
/**
 * Read the memory reader text as a JSON object line, don't consume
 * terminating newline. The line must not start with whitespace.
 *
 * @param mem_json_reader    The memory reader to parse the object for.
 * @param pobject       Location for the parsed object pointer.
 *
 * @return Global return code.
 */
static tlog_grc
tlog_mem_json_reader_read_json(struct tlog_mem_json_reader *mem_json_reader,
                               struct json_object **pobject)
{
    const char *p;
    struct json_object *object;
    enum json_tokener_error jerr;

    assert(tlog_mem_json_reader_is_valid(
                    (struct tlog_json_reader *)mem_json_reader));
    assert(pobject != NULL);

    json_tokener_reset(mem_json_reader->tok);

    /* Look for a terminating newline */
    for (p = mem_json_reader->pos;
         p < mem_json_reader->end && *p != '\n';
         p++);

    /* If the line is empty */
    if (p == mem_json_reader->pos) {
        /* Report EOF */
        *pobject = NULL;
        return TLOG_RC_OK;
    }

    /* Parse the line */
    object = json_tokener_parse_ex(mem_json_reader->tok,
                                   mem_json_reader->pos,
                                   p - mem_json_reader->pos);
    mem_json_reader->pos = p;
    if (object == NULL) {
        jerr = json_tokener_get_error(mem_json_reader->tok);
        return (jerr == json_tokener_continue)
                    ? TLOG_RC_MEM_JSON_READER_INCOMPLETE_LINE
                    : TLOG_GRC_FROM(json, jerr);
    }

    /* Return the parsed object */
    *pobject = object;
    return TLOG_RC_OK;
}
Example #4
0
/**
 * Read a line from the connection and parse it as a json object.
 * The returned object has ref count 1.
 */
json_object *net_json_read(struct net_json *connection){
	read_line(connection->fd, connection->buf);

	json_tokener_reset(connection->tok);

	json_object *obj = json_tokener_parse_ex(connection->tok,
		connection->buf->buf, connection->buf->bpos);
	
	if(obj == NULL){
		if(connection->tok->err != json_tokener_success){
			error(0, "Error in JSON data (%s at offset %d).\n",
				json_tokener_errors[connection->tok->err],
				connection->tok->char_offset);
		}else{
			error(errno, NULL);
		}
	}

	return obj;
}
/**
 * Authenticate against a Keystone authentication service with the given tenant and user names and password.
 * This yields an authorisation token, which is then used to access all Swift services.
 */
enum keystone_error
keystone_authenticate(keystone_context_t *context, const char *url, const char *tenant_name, const char *username, const char *password)
{
	CURLcode curl_err;
	struct curl_slist *headers = NULL;
	size_t body_len;

	assert(context != NULL);
	assert(context->pvt.curl != NULL);
	assert(url != NULL);
	assert(tenant_name != NULL);
	assert(username != NULL);
	assert(password != NULL);

	body_len =
		strlen(KEYSTONE_AUTH_PAYLOAD_BEFORE_USERNAME)
		+ strlen(username)
		+ strlen(KEYSTONE_AUTH_PAYLOAD_BEFORE_PASSWORD)
		+ strlen(password)
		+ strlen(KEYSTONE_AUTH_PAYLOAD_BEFORE_TENANT)
		+ strlen(tenant_name)
		+ strlen(KEYSTONE_AUTH_PAYLOAD_END)
	;

	/* Create or reset the JSON tokeniser */
	if (NULL == context->pvt.json_tokeniser) {
		context->pvt.json_tokeniser = json_tokener_new();
		if (NULL == context->pvt.json_tokeniser) {
			context->keystone_error("json_tokener_new failed", KSERR_INIT_FAILED);
			return KSERR_INIT_FAILED;
		}
	} else {
		json_tokener_reset(context->pvt.json_tokeniser);
	}

	curl_err = curl_easy_setopt(context->pvt.curl, CURLOPT_URL, url);
	if (CURLE_OK != curl_err) {
		context->curl_error("curl_easy_setopt", curl_err);
		return KSERR_URL_FAILED;
	}

	curl_err = curl_easy_setopt(context->pvt.curl, CURLOPT_POST, 1L);
	if (CURLE_OK != curl_err) {
		context->curl_error("curl_easy_setopt", curl_err);
		return KSERR_URL_FAILED;
	}

	/* Append header specifying body content type (since this differs from libcurl's default) */
	headers = curl_slist_append(headers, "Content-Type: " KEYSTONE_AUTH_REQUEST_FORMAT);

	/* Append pseudo-header defeating libcurl's default addition of an "Expect: 100-continue" header. */
	headers = curl_slist_append(headers, "Expect:");

	/* Generate POST request body containing the authentication credentials */
	context->pvt.auth_payload = context->allocator(
		context->pvt.auth_payload,
		body_len
		+ 1 /* '\0' */
	);
	if (NULL == context->pvt.auth_payload) {
		curl_slist_free_all(headers);
		return KSERR_ALLOC_FAILED;
	}
	sprintf(context->pvt.auth_payload, "%s%s%s%s%s%s%s",
		KEYSTONE_AUTH_PAYLOAD_BEFORE_USERNAME,
		username,
		KEYSTONE_AUTH_PAYLOAD_BEFORE_PASSWORD,
		password,
		KEYSTONE_AUTH_PAYLOAD_BEFORE_TENANT,
		tenant_name,
		KEYSTONE_AUTH_PAYLOAD_END
	);

	if (context->pvt.debug) {
		fputs(context->pvt.auth_payload, stderr);
	}

	/* Pass the POST request body to libcurl. The data are not copied, so they must persist during the request lifetime. */
	curl_err = curl_easy_setopt(context->pvt.curl, CURLOPT_POSTFIELDS, context->pvt.auth_payload);
	if (CURLE_OK != curl_err) {
		context->curl_error("curl_easy_setopt", curl_err);
		curl_slist_free_all(headers);
		return KSERR_URL_FAILED;
	}

	curl_err = curl_easy_setopt(context->pvt.curl, CURLOPT_POSTFIELDSIZE, body_len);
	if (CURLE_OK != curl_err) {
		context->curl_error("curl_easy_setopt", curl_err);
		curl_slist_free_all(headers);
		return KSERR_URL_FAILED;
	}

	/* Add header requesting desired response content type */
	headers = curl_slist_append(headers, "Accept: " KEYSTONE_AUTH_RESPONSE_FORMAT);

	curl_err = curl_easy_setopt(context->pvt.curl, CURLOPT_HTTPHEADER, headers);
	if (CURLE_OK != curl_err) {
		context->curl_error("curl_easy_setopt", curl_err);
		curl_slist_free_all(headers);
		return KSERR_URL_FAILED;
	}

	curl_err = curl_easy_setopt(context->pvt.curl, CURLOPT_WRITEFUNCTION, process_keystone_response);
	if (CURLE_OK != curl_err) {
		context->curl_error("curl_easy_setopt", curl_err);
		curl_slist_free_all(headers);
		return KSERR_URL_FAILED;
	}

	curl_err = curl_easy_setopt(context->pvt.curl, CURLOPT_WRITEDATA, context);
	if (CURLE_OK != curl_err) {
		context->curl_error("curl_easy_setopt", curl_err);
		curl_slist_free_all(headers);
		return KSERR_URL_FAILED;
	}

	curl_err = curl_easy_perform(context->pvt.curl);
	if (CURLE_OK != curl_err) {
		context->curl_error("curl_easy_perform", curl_err);
		curl_slist_free_all(headers);
		return KSERR_URL_FAILED;
	}

	curl_slist_free_all(headers);

	if (NULL == context->pvt.auth_token) {
		return KSERR_AUTH_REJECTED;
	}

	return KSERR_SUCCESS;
}
Example #6
0
DWORD WINAPI CollabSocket::recvHandler(void *_sock) {
#else
void *CollabSocket::recvHandler(void *_sock) {
#endif
   static qstring b;
   unsigned char buf[2048];  //read a large chunk, we'll be notified if there is more
   CollabSocket *sock = (CollabSocket*)_sock;
   json_tokener *tok = json_tokener_new();

   while (sock->isConnected()) {
      int len = sock->recv(buf, sizeof(buf) - 1);
      if (len == 0) {
         //end of file
         break;
      }
      if (len < 0) {
#ifdef _WIN32
         //timeouts are okay
         if (WSAGetLastError() == WSAETIMEDOUT) {
            continue;
         }
#else
         //timeouts are okay
         if (errno == EAGAIN || errno == EWOULDBLOCK) {
            continue;
         }
#endif
//       assumption is that socket is borked and next send will fail also
//       maybe should close socket here at a minimum.
//       in any case thread is exiting
//       reconnecting is a better strategy
         break;
      }
      if (sock->_disp) {
         json_object *jobj = NULL;
         enum json_tokener_error jerr;
         buf[len] = 0;
         b.append((char*)buf, len);   //append new data into static buffer

         while (1) {
            jobj = json_tokener_parse_ex(tok, b.c_str(), (int)b.length());
            jerr = json_tokener_get_error(tok);
            if (jerr == json_tokener_continue) {
               //json object is syntactically correct, but incomplete
//               msg("json_tokener_continue for %s\n", b.c_str());
               break;
            }
            else if (jerr != json_tokener_success) {
               //need to reconnect socket and in the meantime start caching event locally
//               msg("jerr != json_tokener_success for %s\n", b.c_str());
               //need to break out of both loops
               goto end_loop;
            }
            else if (jobj != NULL) {
               //we extracted a json object from the front of the string
               //queue it and trim the string
//               msg("json_tokener_success for %s\n", b.c_str());
               if (tok->char_offset < b.length()) {
                  //shift any remaining portions of the buffer to the front
                  b.remove(0, tok->char_offset); 
               }
               else {
                  b.clear();
               }
               sock->drt->queueObject(jobj);
            }
         }
         json_tokener_reset(tok);
      }
   }
end_loop:
   json_tokener_free(tok);
   sock->cleanup();
   return 0;
}

int send_all(const qstring &s) {
   if (comm) {
      return comm->sendAll(s);
   }
   return 0;
}

int send_msg(const qstring &s) {
   if (comm) {
      return comm->sendAll(s);
   }
   else {
      if (changeCache != NULL) {
//         msg("writing to change cache\n");
         *changeCache += s;
         return (int)s.length();
      }
   }
   return 0;
}
Example #7
0
static void test_incremental_parse()
{
	json_object *new_obj;
	enum json_tokener_error jerr;
	json_tokener *tok;
	const char *string_to_parse;
	int ii;
	int num_ok, num_error;

	num_ok = 0;
	num_error = 0;

	printf("Starting incremental tests.\n");
	printf("Note: quotes and backslashes seen in the output here are literal values passed\n");
	printf("     to the parse functions.  e.g. this is 4 characters: \"\\f\"\n");

	string_to_parse = "{ \"foo"; /* } */
	printf("json_tokener_parse(%s) ... ", string_to_parse);
	new_obj = json_tokener_parse(string_to_parse);
	if (new_obj == NULL) printf("got error as expected\n");

	/* test incremental parsing in various forms */
	tok = json_tokener_new();
	for (ii = 0; incremental_steps[ii].string_to_parse != NULL; ii++)
	{
		int this_step_ok = 0;
		struct incremental_step *step = &incremental_steps[ii];
		int length = step->length;
		int expected_char_offset = step->char_offset;

		if (step->reset_tokener & 2)
			json_tokener_set_flags(tok, JSON_TOKENER_STRICT);
		else
			json_tokener_set_flags(tok, 0);

		if (length == -1)
			length = strlen(step->string_to_parse);
		if (expected_char_offset == -1)
			expected_char_offset = length;

		printf("json_tokener_parse_ex(tok, %-12s, %3d) ... ",
			step->string_to_parse, length);
		new_obj = json_tokener_parse_ex(tok, step->string_to_parse, length);

		jerr = json_tokener_get_error(tok);
		if (step->expected_error != json_tokener_success)
		{
			if (new_obj != NULL)
				printf("ERROR: invalid object returned: %s\n",
					json_object_to_json_string(new_obj));
			else if (jerr != step->expected_error)
				printf("ERROR: got wrong error: %s\n",
					json_tokener_error_desc(jerr));
			else if (tok->char_offset != expected_char_offset)
				printf("ERROR: wrong char_offset %d != expected %d\n",
					tok->char_offset,
					expected_char_offset);
			else
			{
				printf("OK: got correct error: %s\n", json_tokener_error_desc(jerr));
				this_step_ok = 1;
			}
		}
		else
		{
			if (new_obj == NULL)
				printf("ERROR: expected valid object, instead: %s\n",
					json_tokener_error_desc(jerr));
			else if (tok->char_offset != expected_char_offset)
				printf("ERROR: wrong char_offset %d != expected %d\n",
					tok->char_offset,
					expected_char_offset);
			else
			{
				printf("OK: got object of type [%s]: %s\n",
					json_type_to_name(json_object_get_type(new_obj)),
					json_object_to_json_string(new_obj));
				this_step_ok = 1;
			}
		}

		if (new_obj)
			json_object_put(new_obj);

		if (step->reset_tokener & 1)
			json_tokener_reset(tok);

		if (this_step_ok)
			num_ok++;
		else
			num_error++;
	}

	json_tokener_free(tok);

	printf("End Incremental Tests OK=%d ERROR=%d\n", num_ok, num_error);

	return;
}
Example #8
0
/**
 * Read the fd reader text as (a part of) a JSON object line, don't consume
 * terminating newline.
 *
 * @param fd_json_reader     The fd reader to parse the object for.
 * @param pobject       Location for the parsed object pointer.
 *
 * @return Global return code.
 */
static tlog_grc
tlog_fd_json_reader_read_json(struct tlog_fd_json_reader *fd_json_reader,
                              struct json_object **pobject)
{
    tlog_grc grc;
    char *p;
    struct json_object *object;
    enum json_tokener_error jerr;
    bool got_text = false;

    assert(tlog_fd_json_reader_is_valid(
                    (struct tlog_json_reader *)fd_json_reader));
    assert(pobject != NULL);

    json_tokener_reset(fd_json_reader->tok);

    /* Until EOF */
    do {
        /* If the buffer is not empty */
        if (fd_json_reader->pos < fd_json_reader->end) {
            /* We got something to parse */
            got_text = true;

            /* Look for a terminating newline */
            for (p = fd_json_reader->pos;
                 p < fd_json_reader->end && *p != '\n';
                 p++);

            /* Parse the next piece */
            object = json_tokener_parse_ex(fd_json_reader->tok,
                                           fd_json_reader->pos,
                                           p - fd_json_reader->pos);
            fd_json_reader->pos = p;

            /* If we finished parsing an object */
            if (object != NULL) {
                *pobject = object;
                return TLOG_RC_OK;
            } else {
                jerr = json_tokener_get_error(fd_json_reader->tok);
                /* If object is not finished */
                if (jerr == json_tokener_continue) {
                    /* If we encountered an object-terminating newline */
                    if (p < fd_json_reader->end) {
                        return TLOG_RC_FD_JSON_READER_INCOMPLETE_LINE;
                    }
                } else {
                    return TLOG_GRC_FROM(json, jerr);
                }
            }
        }

        grc = tlog_fd_json_reader_refill_buf(fd_json_reader);
        if (grc != TLOG_RC_OK)
            return grc;
    } while (fd_json_reader->end > fd_json_reader->buf);

    if (got_text) {
        return TLOG_RC_FD_JSON_READER_INCOMPLETE_LINE;
    } else {
        *pobject = NULL;
        return TLOG_RC_OK;
    }
}