Allow stubbing based on query xmlns
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -43,5 +43,6 @@ configure.lineno
|
||||
src/server/prime.lo
|
||||
src/server/verify.lo
|
||||
src/server/httpapi.lo
|
||||
src/server/stream_parser.lo
|
||||
|
||||
stabbertest
|
||||
|
||||
@@ -4,6 +4,7 @@ sources = \
|
||||
src/server/server.c src/server/server.h \
|
||||
src/server/httpapi.c src/server/httpapi.h \
|
||||
src/server/xmppclient.c src/server/xmppclient.h \
|
||||
src/server/stream_parser.c src/server/stream_parser.h \
|
||||
src/server/parser.c src/server/parser.h \
|
||||
src/server/stanza.c src/server/stanza.h \
|
||||
src/server/log.c src/server/log.h \
|
||||
|
||||
@@ -58,6 +58,13 @@ stbbr_for_id(char *id, char *stream)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int
|
||||
stbbr_for_query(char *query, char *stream)
|
||||
{
|
||||
prime_for_query(query, stream);
|
||||
return 1;
|
||||
}
|
||||
|
||||
void
|
||||
stbbr_wait_for(char *id)
|
||||
{
|
||||
|
||||
@@ -20,36 +20,18 @@
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <expat.h>
|
||||
#include <glib.h>
|
||||
|
||||
#include "server/parser.h"
|
||||
#include "server/stanza.h"
|
||||
#include "server/log.h"
|
||||
|
||||
static int depth = 0;
|
||||
static int do_reset = 0;
|
||||
|
||||
static XML_Parser parser;
|
||||
static XMPPStanza *curr_stanza;
|
||||
static GString *curr_string = NULL;
|
||||
|
||||
static stream_start_func stream_start_cb = NULL;
|
||||
static auth_func auth_cb = NULL;
|
||||
static id_func id_cb = NULL;
|
||||
static XMPPStanza *curr_stanza = NULL;
|
||||
|
||||
static void
|
||||
start_element(void *data, const char *element, const char **attributes)
|
||||
{
|
||||
if (g_strcmp0(element, "stream:stream") == 0) {
|
||||
log_println("RECV: %s", curr_string->str);
|
||||
stream_start_cb();
|
||||
do_reset = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
XMPPStanza *stanza = stanza_new(element, attributes);
|
||||
|
||||
if (depth == 0) {
|
||||
@@ -71,19 +53,6 @@ end_element(void *data, const char *element)
|
||||
if (depth > 0) {
|
||||
stanza_add_child(curr_stanza->parent, curr_stanza);
|
||||
curr_stanza = curr_stanza->parent;
|
||||
} else {
|
||||
log_println("RECV: %s", curr_string->str);
|
||||
stanza_add(curr_stanza);
|
||||
if (stanza_get_child_by_ns(curr_stanza, "jabber:iq:auth")) {
|
||||
auth_cb(curr_stanza);
|
||||
} else {
|
||||
const char *id = stanza_get_id(curr_stanza);
|
||||
if (id) {
|
||||
id_cb(id);
|
||||
}
|
||||
}
|
||||
|
||||
do_reset = 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,45 +66,19 @@ handle_data(void *data, const char *content, int length)
|
||||
g_string_append_len(curr_stanza->content, content, length);
|
||||
}
|
||||
|
||||
void
|
||||
parser_init(stream_start_func startcb, auth_func authcb, id_func idcb)
|
||||
XMPPStanza *
|
||||
parse_stanza(char *stanza_text)
|
||||
{
|
||||
if (curr_string) {
|
||||
g_string_free(curr_string, TRUE);
|
||||
depth = 0;
|
||||
if (curr_stanza) {
|
||||
stanza_free(curr_stanza);
|
||||
}
|
||||
curr_string = g_string_new("");
|
||||
|
||||
stream_start_cb = startcb;
|
||||
auth_cb = authcb;
|
||||
id_cb = idcb;
|
||||
|
||||
parser = XML_ParserCreate(NULL);
|
||||
XML_Parser parser = XML_ParserCreate(NULL);
|
||||
XML_SetElementHandler(parser, start_element, end_element);
|
||||
XML_SetCharacterDataHandler(parser, handle_data);
|
||||
}
|
||||
|
||||
int
|
||||
parser_feed(char *chunk, int len)
|
||||
{
|
||||
g_string_append_len(curr_string, chunk, len);
|
||||
int res = XML_Parse(parser, chunk, len, 0);
|
||||
parser_reset();
|
||||
return res;
|
||||
}
|
||||
|
||||
void
|
||||
parser_close(void)
|
||||
{
|
||||
XML_Parse(parser, stanza_text, strlen(stanza_text), 0);
|
||||
XML_ParserFree(parser);
|
||||
parser = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
parser_reset(void)
|
||||
{
|
||||
if (do_reset == 1) {
|
||||
parser_close();
|
||||
parser_init(stream_start_cb, auth_cb, id_cb);
|
||||
do_reset = 0;
|
||||
}
|
||||
return curr_stanza;
|
||||
}
|
||||
|
||||
@@ -24,16 +24,7 @@
|
||||
#define __H_PARSER
|
||||
|
||||
#include "server/stanza.h"
|
||||
#include "server/xmppclient.h"
|
||||
|
||||
typedef void (*stream_start_func)(void);
|
||||
typedef void (*auth_func)(XMPPStanza *stanza);
|
||||
typedef void (*id_func)(const char *id);
|
||||
|
||||
void parser_init(stream_start_func startcb, auth_func authcb, id_func);
|
||||
int parser_feed(char *chunk, int len);
|
||||
void parser_close(void);
|
||||
void parser_reset(void);
|
||||
void parser_show_stanzas(void);
|
||||
XMPPStanza* parse_stanza(char *stanza_text);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -25,14 +25,19 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "server/stanza.h"
|
||||
#include "server/parser.h"
|
||||
|
||||
static char *required_passwd = NULL;
|
||||
static GHashTable *idstubs = NULL;
|
||||
static GHashTable *querystubs = NULL;
|
||||
|
||||
void
|
||||
prime_init(void)
|
||||
{
|
||||
required_passwd = strdup("password");
|
||||
idstubs = g_hash_table_new_full(g_str_hash, g_str_equal, free, free);
|
||||
querystubs = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)stanza_free);
|
||||
}
|
||||
|
||||
void
|
||||
@@ -44,7 +49,12 @@ prime_free_all(void)
|
||||
if (idstubs) {
|
||||
g_hash_table_destroy(idstubs);
|
||||
}
|
||||
idstubs = NULL;
|
||||
|
||||
querystubs = NULL;
|
||||
if (querystubs) {
|
||||
g_hash_table_destroy(querystubs);
|
||||
}
|
||||
querystubs = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
@@ -69,7 +79,22 @@ prime_for_id(const char *id, char *stream)
|
||||
}
|
||||
|
||||
char*
|
||||
prime_get_for(const char *id)
|
||||
prime_get_for_id(const char *id)
|
||||
{
|
||||
return g_hash_table_lookup(idstubs, id);
|
||||
}
|
||||
|
||||
void
|
||||
prime_for_query(const char *query, char *stream)
|
||||
{
|
||||
if (querystubs) {
|
||||
XMPPStanza *stanza = parse_stanza(stream);
|
||||
g_hash_table_insert(querystubs, strdup(query), stanza);
|
||||
}
|
||||
}
|
||||
|
||||
XMPPStanza *
|
||||
prime_get_for_query(const char *query)
|
||||
{
|
||||
return g_hash_table_lookup(querystubs, query);
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@
|
||||
#ifndef __H_PRIME
|
||||
#define __H_PRIME
|
||||
|
||||
#include "server/stanza.h"
|
||||
|
||||
void prime_init(void);
|
||||
void prime_free_all(void);
|
||||
|
||||
@@ -30,6 +32,9 @@ void prime_required_passwd(char *password);
|
||||
char* prime_get_passwd(void);
|
||||
|
||||
int prime_for_id(const char *id, char *stream);
|
||||
char* prime_get_for(const char *id);
|
||||
char* prime_get_for_id(const char *id);
|
||||
|
||||
int prime_for_query(const char *query, char *stream);
|
||||
XMPPStanza* prime_get_for_query(const char *query);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -29,7 +29,7 @@
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "server/xmppclient.h"
|
||||
#include "server/parser.h"
|
||||
#include "server/stream_parser.h"
|
||||
#include "server/prime.h"
|
||||
#include "server/stanza.h"
|
||||
#include "server/verify.h"
|
||||
@@ -216,13 +216,28 @@ auth_callback(XMPPStanza *stanza)
|
||||
void
|
||||
id_callback(const char *id)
|
||||
{
|
||||
char *stream = prime_get_for(id);
|
||||
char *stream = prime_get_for_id(id);
|
||||
if (stream) {
|
||||
log_println("--> ID callback fired for '%s'", id);
|
||||
write_stream(stream);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
query_callback(const char *query, const char *id)
|
||||
{
|
||||
XMPPStanza *stanza = prime_get_for_query(query);
|
||||
if (stanza) {
|
||||
log_println("--> QUERY callback fired for '%s'", query);
|
||||
stanza_set_id(stanza, id);
|
||||
char *stream = stanza_to_string(stanza);
|
||||
write_stream(stream);
|
||||
|
||||
stanza_free(stanza);
|
||||
free(stream);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
server_wait_for(char *id)
|
||||
{
|
||||
@@ -288,7 +303,7 @@ _start_server_cb(void* userdata)
|
||||
}
|
||||
|
||||
client = xmppclient_new(client_addr, client_socket);
|
||||
parser_init(stream_start_callback, auth_callback, id_callback);
|
||||
parser_init(stream_start_callback, auth_callback, id_callback, query_callback);
|
||||
|
||||
read_stream();
|
||||
|
||||
|
||||
@@ -96,6 +96,58 @@ stanza_show_all(void)
|
||||
pthread_mutex_unlock(&stanzas_lock);
|
||||
}
|
||||
|
||||
char *
|
||||
stanza_to_string(XMPPStanza *stanza)
|
||||
{
|
||||
GString *stanza_str = g_string_new("<");
|
||||
|
||||
g_string_append(stanza_str, stanza->name);
|
||||
|
||||
if (stanza->attrs) {
|
||||
GList *curr = stanza->attrs;
|
||||
while (curr) {
|
||||
XMPPAttr *attr = curr->data;
|
||||
g_string_append(stanza_str, " ");
|
||||
g_string_append(stanza_str, attr->name);
|
||||
g_string_append(stanza_str, "=\"");
|
||||
g_string_append(stanza_str, attr->value);
|
||||
g_string_append(stanza_str, "\"");
|
||||
|
||||
curr = g_list_next(curr);
|
||||
}
|
||||
}
|
||||
|
||||
if (stanza->content) {
|
||||
g_string_append(stanza_str, ">");
|
||||
g_string_append(stanza_str, stanza->content->str);
|
||||
g_string_append(stanza_str, "</");
|
||||
g_string_append(stanza_str, stanza->name);
|
||||
g_string_append(stanza_str, ">");
|
||||
} else if (stanza->children) {
|
||||
g_string_append(stanza_str, ">");
|
||||
|
||||
GList *curr = stanza->children;
|
||||
while (curr) {
|
||||
XMPPStanza *child = curr->data;
|
||||
char *child_str = stanza_to_string(child);
|
||||
g_string_append(stanza_str, child_str);
|
||||
free(child_str);
|
||||
curr = g_list_next(curr);
|
||||
}
|
||||
|
||||
g_string_append(stanza_str, "</");
|
||||
g_string_append(stanza_str, stanza->name);
|
||||
g_string_append(stanza_str, ">");
|
||||
} else {
|
||||
g_string_append(stanza_str, "/>");
|
||||
}
|
||||
|
||||
char *result = stanza_str->str;
|
||||
g_string_free(stanza_str, FALSE);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int
|
||||
stanzas_contains_id(char *id)
|
||||
{
|
||||
@@ -214,6 +266,72 @@ stanza_get_id(XMPPStanza *stanza)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void
|
||||
stanza_set_id(XMPPStanza *stanza, const char *id)
|
||||
{
|
||||
GList *curr_attr = stanza->attrs;
|
||||
while (curr_attr) {
|
||||
XMPPAttr *attr = curr_attr->data;
|
||||
if (g_strcmp0(attr->name, "id") == 0) {
|
||||
free(attr->value);
|
||||
attr->value = strdup(id);
|
||||
return;
|
||||
}
|
||||
|
||||
curr_attr = g_list_next(curr_attr);
|
||||
}
|
||||
|
||||
XMPPAttr *attrnew = malloc(sizeof(XMPPAttr));
|
||||
attrnew->name = strdup("id");
|
||||
attrnew->value = strdup(id);
|
||||
stanza->attrs = g_list_append(stanza->attrs, attrnew);
|
||||
}
|
||||
|
||||
const char *
|
||||
stanza_get_attr(XMPPStanza *stanza, const char *name)
|
||||
{
|
||||
if (!stanza->attrs) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
GList *curr_attr = stanza->attrs;
|
||||
while (curr_attr) {
|
||||
XMPPAttr *attr = curr_attr->data;
|
||||
if (g_strcmp0(attr->name, name) == 0) {
|
||||
return attr->value;
|
||||
}
|
||||
|
||||
curr_attr = g_list_next(curr_attr);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *
|
||||
stanza_get_query_request(XMPPStanza *stanza)
|
||||
{
|
||||
if (g_strcmp0(stanza->name, "iq") != 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *type = stanza_get_attr(stanza, "type");
|
||||
if (g_strcmp0(type, "get") != 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
XMPPStanza *query = stanza_get_child_by_name(stanza, "query");
|
||||
if (!query) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char *xmlns = stanza_get_attr(query, "xmlns");
|
||||
if (!xmlns) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return xmlns;
|
||||
}
|
||||
|
||||
static int
|
||||
_xmpp_attr_equal(XMPPAttr *attr1, XMPPAttr *attr2)
|
||||
{
|
||||
|
||||
@@ -44,6 +44,8 @@ void stanza_show_all(void);
|
||||
void stanza_add(XMPPStanza *stanza);
|
||||
void stanza_add_child(XMPPStanza *parent, XMPPStanza *child);
|
||||
const char* stanza_get_id(XMPPStanza *stanza);
|
||||
void stanza_set_id(XMPPStanza *stanza, const char *id);
|
||||
const char *stanza_get_query_request(XMPPStanza *stanza);
|
||||
|
||||
XMPPStanza* stanza_get_child_by_ns(XMPPStanza *stanza, char *ns);
|
||||
XMPPStanza* stanza_get_child_by_name(XMPPStanza *stanza, char *name);
|
||||
@@ -56,4 +58,6 @@ int stanzas_contains_id(char *id);
|
||||
void stanza_free(XMPPStanza *stanza);
|
||||
void stanza_free_all(void);
|
||||
|
||||
char* stanza_to_string(XMPPStanza *stanza);
|
||||
|
||||
#endif
|
||||
|
||||
147
src/server/stream_parser.c
Normal file
147
src/server/stream_parser.c
Normal file
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* stream_parser.c
|
||||
*
|
||||
* Copyright (C) 2015 James Booth <boothj5@gmail.com>
|
||||
*
|
||||
* This file is part of Stabber.
|
||||
*
|
||||
* Stabber is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Stabber is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Stabber. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <expat.h>
|
||||
#include <glib.h>
|
||||
|
||||
#include "server/stream_parser.h"
|
||||
#include "server/stanza.h"
|
||||
#include "server/log.h"
|
||||
|
||||
static int depth = 0;
|
||||
static int do_reset = 0;
|
||||
|
||||
static XML_Parser parser;
|
||||
static XMPPStanza *curr_stanza;
|
||||
static GString *curr_string = NULL;
|
||||
|
||||
static stream_start_func stream_start_cb = NULL;
|
||||
static auth_func auth_cb = NULL;
|
||||
static id_func id_cb = NULL;
|
||||
static query_func query_cb = NULL;
|
||||
|
||||
static void
|
||||
start_element(void *data, const char *element, const char **attributes)
|
||||
{
|
||||
if (g_strcmp0(element, "stream:stream") == 0) {
|
||||
log_println("RECV: %s", curr_string->str);
|
||||
stream_start_cb();
|
||||
do_reset = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
XMPPStanza *stanza = stanza_new(element, attributes);
|
||||
|
||||
if (depth == 0) {
|
||||
curr_stanza = stanza;
|
||||
curr_stanza->parent = NULL;
|
||||
} else {
|
||||
stanza->parent = curr_stanza;
|
||||
curr_stanza = stanza;
|
||||
}
|
||||
|
||||
depth++;
|
||||
}
|
||||
|
||||
static void
|
||||
end_element(void *data, const char *element)
|
||||
{
|
||||
depth--;
|
||||
|
||||
if (depth > 0) {
|
||||
stanza_add_child(curr_stanza->parent, curr_stanza);
|
||||
curr_stanza = curr_stanza->parent;
|
||||
} else {
|
||||
log_println("RECV: %s", curr_string->str);
|
||||
stanza_add(curr_stanza);
|
||||
if (stanza_get_child_by_ns(curr_stanza, "jabber:iq:auth")) {
|
||||
auth_cb(curr_stanza);
|
||||
} else {
|
||||
const char *id = stanza_get_id(curr_stanza);
|
||||
if (id) {
|
||||
id_cb(id);
|
||||
}
|
||||
const char *query = stanza_get_query_request(curr_stanza);
|
||||
if (query) {
|
||||
query_cb(query, id);
|
||||
}
|
||||
}
|
||||
|
||||
do_reset = 1;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
handle_data(void *data, const char *content, int length)
|
||||
{
|
||||
if (!curr_stanza->content) {
|
||||
curr_stanza->content = g_string_new("");
|
||||
}
|
||||
|
||||
g_string_append_len(curr_stanza->content, content, length);
|
||||
}
|
||||
|
||||
void
|
||||
parser_init(stream_start_func startcb, auth_func authcb, id_func idcb, query_func querycb)
|
||||
{
|
||||
if (curr_string) {
|
||||
g_string_free(curr_string, TRUE);
|
||||
}
|
||||
curr_string = g_string_new("");
|
||||
|
||||
stream_start_cb = startcb;
|
||||
auth_cb = authcb;
|
||||
id_cb = idcb;
|
||||
query_cb = querycb;
|
||||
|
||||
parser = XML_ParserCreate(NULL);
|
||||
XML_SetElementHandler(parser, start_element, end_element);
|
||||
XML_SetCharacterDataHandler(parser, handle_data);
|
||||
}
|
||||
|
||||
int
|
||||
parser_feed(char *chunk, int len)
|
||||
{
|
||||
g_string_append_len(curr_string, chunk, len);
|
||||
int res = XML_Parse(parser, chunk, len, 0);
|
||||
parser_reset();
|
||||
return res;
|
||||
}
|
||||
|
||||
void
|
||||
parser_close(void)
|
||||
{
|
||||
XML_ParserFree(parser);
|
||||
parser = NULL;
|
||||
}
|
||||
|
||||
void
|
||||
parser_reset(void)
|
||||
{
|
||||
if (do_reset == 1) {
|
||||
parser_close();
|
||||
parser_init(stream_start_cb, auth_cb, id_cb, query_cb);
|
||||
do_reset = 0;
|
||||
}
|
||||
}
|
||||
40
src/server/stream_parser.h
Normal file
40
src/server/stream_parser.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* stream_parser.h
|
||||
*
|
||||
* Copyright (C) 2015 James Booth <boothj5@gmail.com>
|
||||
*
|
||||
* This file is part of Stabber.
|
||||
*
|
||||
* Stabber is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Stabber is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Stabber. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __H_STREAM_PARSER
|
||||
#define __H_STREAM_PARSER
|
||||
|
||||
#include "server/stanza.h"
|
||||
#include "server/xmppclient.h"
|
||||
|
||||
typedef void (*stream_start_func)(void);
|
||||
typedef void (*auth_func)(XMPPStanza *stanza);
|
||||
typedef void (*id_func)(const char *id);
|
||||
typedef void (*query_func)(const char *query, const char *id);
|
||||
|
||||
void parser_init(stream_start_func startcb, auth_func authcb, id_func idcb, query_func querycb);
|
||||
int parser_feed(char *chunk, int len);
|
||||
void parser_close(void);
|
||||
void parser_reset(void);
|
||||
void parser_show_stanzas(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user