2015-07-06 00:21:58 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015 Glyptodon LLC
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "guac_json.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <guacamole/protocol.h>
|
|
|
|
#include <guacamole/socket.h>
|
|
|
|
#include <guacamole/stream.h>
|
2016-03-01 05:38:51 +00:00
|
|
|
#include <guacamole/user.h>
|
2015-07-06 00:21:58 +00:00
|
|
|
|
2016-03-01 05:38:51 +00:00
|
|
|
void guac_common_json_flush(guac_user* user, guac_stream* stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
guac_common_json_state* json_state) {
|
|
|
|
|
|
|
|
/* If JSON buffer is non-empty, write contents to blob and reset */
|
|
|
|
if (json_state->size > 0) {
|
2016-03-01 05:38:51 +00:00
|
|
|
guac_protocol_send_blob(user->socket, stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
json_state->buffer, json_state->size);
|
|
|
|
|
|
|
|
/* Reset JSON buffer size */
|
|
|
|
json_state->size = 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:38:51 +00:00
|
|
|
int guac_common_json_write(guac_user* user, guac_stream* stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
guac_common_json_state* json_state, const char* buffer, int length) {
|
|
|
|
|
2015-07-06 06:26:40 +00:00
|
|
|
int blob_written = 0;
|
2015-07-06 00:21:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Append to and flush the JSON buffer as necessary to write the given
|
|
|
|
* data
|
|
|
|
*/
|
|
|
|
while (length > 0) {
|
|
|
|
|
|
|
|
/* Ensure provided data does not exceed size of buffer */
|
|
|
|
int blob_length = length;
|
|
|
|
if (blob_length > sizeof(json_state->buffer))
|
|
|
|
blob_length = sizeof(json_state->buffer);
|
|
|
|
|
|
|
|
/* Flush if more room is needed */
|
|
|
|
if (json_state->size + blob_length > sizeof(json_state->buffer)) {
|
2016-03-01 05:38:51 +00:00
|
|
|
guac_common_json_flush(user, stream, json_state);
|
2015-07-06 06:26:40 +00:00
|
|
|
blob_written = 1;
|
2015-07-06 00:21:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Append data to JSON buffer */
|
|
|
|
memcpy(json_state->buffer + json_state->size,
|
|
|
|
buffer, blob_length);
|
|
|
|
|
|
|
|
json_state->size += blob_length;
|
|
|
|
|
|
|
|
/* Advance to next blob of data */
|
|
|
|
buffer += blob_length;
|
|
|
|
length -= blob_length;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return blob_written;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:38:51 +00:00
|
|
|
int guac_common_json_write_string(guac_user* user,
|
2015-07-06 00:21:58 +00:00
|
|
|
guac_stream* stream, guac_common_json_state* json_state,
|
|
|
|
const char* str) {
|
|
|
|
|
2015-07-06 06:26:40 +00:00
|
|
|
int blob_written = 0;
|
2015-07-06 00:21:58 +00:00
|
|
|
|
|
|
|
/* Write starting quote */
|
2016-03-01 05:38:51 +00:00
|
|
|
blob_written |= guac_common_json_write(user, stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
json_state, "\"", 1);
|
|
|
|
|
|
|
|
/* Write given string, escaping as necessary */
|
|
|
|
const char* current = str;
|
|
|
|
for (; *current != '\0'; current++) {
|
|
|
|
|
|
|
|
/* Escape all quotes */
|
|
|
|
if (*current == '"') {
|
|
|
|
|
|
|
|
/* Write any string content up to current character */
|
|
|
|
if (current != str)
|
2016-03-01 05:38:51 +00:00
|
|
|
blob_written |= guac_common_json_write(user, stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
json_state, str, current - str);
|
|
|
|
|
|
|
|
/* Escape the quote that was just read */
|
2016-03-01 05:38:51 +00:00
|
|
|
blob_written |= guac_common_json_write(user, stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
json_state, "\\", 1);
|
|
|
|
|
|
|
|
/* Reset string */
|
|
|
|
str = current;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write any remaining string content */
|
|
|
|
if (current != str)
|
2016-03-01 05:38:51 +00:00
|
|
|
blob_written |= guac_common_json_write(user, stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
json_state, str, current - str);
|
|
|
|
|
|
|
|
/* Write ending quote */
|
2016-03-01 05:38:51 +00:00
|
|
|
blob_written |= guac_common_json_write(user, stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
json_state, "\"", 1);
|
|
|
|
|
|
|
|
return blob_written;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:38:51 +00:00
|
|
|
int guac_common_json_write_property(guac_user* user, guac_stream* stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
guac_common_json_state* json_state, const char* name,
|
|
|
|
const char* value) {
|
|
|
|
|
2015-07-06 06:26:40 +00:00
|
|
|
int blob_written = 0;
|
2015-07-06 00:21:58 +00:00
|
|
|
|
|
|
|
/* Write leading comma if not first property */
|
|
|
|
if (json_state->properties_written != 0)
|
2016-03-01 05:38:51 +00:00
|
|
|
blob_written |= guac_common_json_write(user, stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
json_state, ",", 1);
|
|
|
|
|
|
|
|
/* Write property name */
|
2016-03-01 05:38:51 +00:00
|
|
|
blob_written |= guac_common_json_write_string(user, stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
json_state, name);
|
|
|
|
|
|
|
|
/* Separate name from value with colon */
|
2016-03-01 05:38:51 +00:00
|
|
|
blob_written |= guac_common_json_write(user, stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
json_state, ":", 1);
|
|
|
|
|
|
|
|
/* Write property value */
|
2016-03-01 05:38:51 +00:00
|
|
|
blob_written |= guac_common_json_write_string(user, stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
json_state, value);
|
|
|
|
|
|
|
|
json_state->properties_written++;
|
|
|
|
|
|
|
|
return blob_written;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:38:51 +00:00
|
|
|
void guac_common_json_begin_object(guac_user* user, guac_stream* stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
guac_common_json_state* json_state) {
|
|
|
|
|
|
|
|
/* Init JSON state */
|
|
|
|
json_state->size = 0;
|
|
|
|
json_state->properties_written = 0;
|
|
|
|
|
|
|
|
/* Write leading brace - no blob can possibly be written by this */
|
2016-03-01 05:38:51 +00:00
|
|
|
assert(!guac_common_json_write(user, stream, json_state, "{", 1));
|
2015-07-06 00:21:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:38:51 +00:00
|
|
|
int guac_common_json_end_object(guac_user* user, guac_stream* stream,
|
2015-07-06 00:21:58 +00:00
|
|
|
guac_common_json_state* json_state) {
|
|
|
|
|
|
|
|
/* Write final brace of JSON object */
|
2016-03-01 05:38:51 +00:00
|
|
|
return guac_common_json_write(user, stream, json_state, "}", 1);
|
2015-07-06 00:21:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|