Parse XML
This commit is contained in:
7
.gitignore
vendored
7
.gitignore
vendored
@@ -1,3 +1,5 @@
|
||||
core
|
||||
server
|
||||
Makefile
|
||||
Makefile.in
|
||||
aclocal.m4
|
||||
@@ -13,11 +15,12 @@ src/server/.deps/
|
||||
src/server/.dirstamp
|
||||
src/server/clients.o
|
||||
src/server/server.o
|
||||
src/server/parser.o
|
||||
src/server/xmppclient.o
|
||||
|
||||
src/stamp-h1
|
||||
|
||||
.codelite/
|
||||
stabber.project
|
||||
stabber.workspace
|
||||
|
||||
server
|
||||
|
||||
|
||||
@@ -1,2 +1,2 @@
|
||||
bin_PROGRAMS = server
|
||||
server_SOURCES = src/server/server.c src/server/xmppclient.h src/server/xmppclient.c
|
||||
server_SOURCES = src/server/server.c src/server/xmppclient.h src/server/xmppclient.c src/server/parser.h src/server/parser.c
|
||||
|
||||
@@ -14,10 +14,13 @@ AC_PROG_CC
|
||||
PKG_CHECK_MODULES([glib], [glib-2.0 >= 2.26], [],
|
||||
[AC_MSG_ERROR([glib 2.26 or higher is required])])
|
||||
|
||||
PKG_CHECK_MODULES([expat], [expat >= 2.0.0], [],
|
||||
[AC_MSG_ERROR([expat 2.0.0 or higher is required])])
|
||||
|
||||
AM_CFLAGS="-Wall -Wno-deprecated-declarations"
|
||||
AM_CFLAGS="$AM_CFLAGS -Wunused -Werror"
|
||||
AM_CPPFLAGS="$AM_CPPFLAGS $glib_CFLAGS"
|
||||
LIBS="$glib_LIBS $LIBS"
|
||||
AM_CPPFLAGS="$AM_CPPFLAGS $glib_CFLAGS $expat_CFLAGS"
|
||||
LIBS="$glib_LIBS $expat_LIBS $LIBS"
|
||||
|
||||
AC_SUBST(AM_CFLAGS)
|
||||
AC_SUBST(AM_CPPFLAGS)
|
||||
|
||||
91
src/server/parser.c
Normal file
91
src/server/parser.c
Normal file
@@ -0,0 +1,91 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <expat.h>
|
||||
#include <glib.h>
|
||||
|
||||
#include "server/parser.h"
|
||||
|
||||
static int depth = 0;
|
||||
static int do_reset = 0;
|
||||
GString *text = NULL;
|
||||
static XML_Parser parser;
|
||||
|
||||
static void
|
||||
start_element(void *data, const char *element, const char **attribute)
|
||||
{
|
||||
int i;
|
||||
|
||||
printf("LEVEL: %d\n", depth);
|
||||
printf("ELEM: %s\n", element);
|
||||
|
||||
for (i = 0; attribute[i]; i += 2) {
|
||||
printf(" ATTR: %s='%s'\n", attribute[i], attribute[i + 1]);
|
||||
}
|
||||
|
||||
depth++;
|
||||
}
|
||||
|
||||
static void
|
||||
end_element(void *data, const char *element)
|
||||
{
|
||||
// int i;
|
||||
// for (i = 0; i < depth; i++) {
|
||||
// printf(" ");
|
||||
// }
|
||||
|
||||
// if (last_content) {
|
||||
// printf("%s Contents: \"%s\"\n", element, last_content);
|
||||
// }
|
||||
//
|
||||
if (text) {
|
||||
printf(" TEXT: %s\n", text->str);
|
||||
g_string_free(text, TRUE);
|
||||
text = NULL;
|
||||
}
|
||||
|
||||
depth--;
|
||||
if (depth == 0) {
|
||||
do_reset = 1;
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
handle_data(void *data, const char *content, int length)
|
||||
{
|
||||
if (!text) {
|
||||
text = g_string_new("");
|
||||
}
|
||||
|
||||
g_string_append_len(text, content, length);
|
||||
}
|
||||
|
||||
void
|
||||
parser_init(void)
|
||||
{
|
||||
parser = XML_ParserCreate(NULL);
|
||||
XML_SetElementHandler(parser, start_element, end_element);
|
||||
XML_SetCharacterDataHandler(parser, handle_data);
|
||||
}
|
||||
|
||||
int
|
||||
parser_feed(char *chunk, int len)
|
||||
{
|
||||
return XML_Parse(parser, chunk, len, 0);
|
||||
}
|
||||
|
||||
void
|
||||
parser_close(void)
|
||||
{
|
||||
XML_ParserFree(parser);
|
||||
}
|
||||
|
||||
void
|
||||
parser_reset(void)
|
||||
{
|
||||
if (do_reset == 1) {
|
||||
parser_close();
|
||||
parser_init();
|
||||
do_reset = 0;
|
||||
}
|
||||
}
|
||||
9
src/server/parser.h
Normal file
9
src/server/parser.h
Normal file
@@ -0,0 +1,9 @@
|
||||
#ifndef __H_PARSER
|
||||
#define __H_PARSER
|
||||
|
||||
void parser_init(void);
|
||||
int parser_feed(char *chunk, int len);
|
||||
void parser_close(void);
|
||||
void parser_reset(void);
|
||||
|
||||
#endif
|
||||
@@ -6,8 +6,10 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "server/xmppclient.h"
|
||||
#include "server/parser.h"
|
||||
|
||||
#define STREAM_REQ "<?xml version=\"1.0\"?><stream:stream to=\"localhost\" xml:lang=\"en\" version=\"1.0\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\">"
|
||||
#define XML_START "<?xml version=\"1.0\"?>"
|
||||
#define STREAM_REQ "<stream:stream to=\"localhost\" xml:lang=\"en\" version=\"1.0\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\">"
|
||||
#define STREAM_RESP "<?xml version=\"1.0\"?><stream:stream from=\"localhost\" id=\"stream1\" xml:lang=\"en\" version=\"1.0\" xmlns=\"jabber:client\" xmlns:stream=\"http://etherx.jabber.org/streams\">"
|
||||
#define FEATURES "<stream:features></stream:features>"
|
||||
#define AUTH_REQ "<iq id=\"_xmpp_auth1\" type=\"set\"><query xmlns=\"jabber:iq:auth\"><username>stabber</username><password>password</password><resource>profanity</resource></query></iq>"
|
||||
@@ -72,14 +74,12 @@ debug_client(XMPPClient *client)
|
||||
char buf[2];
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
printf("RECV: ");
|
||||
fflush(stdout);
|
||||
|
||||
GString *stream = g_string_new("");
|
||||
errno = 0;
|
||||
while ((read_size = recv(client->sock, buf, 1, 0)) > 0) {
|
||||
printf("%c", buf[0]);
|
||||
parser_feed(buf, 1);
|
||||
fflush(stdout);
|
||||
parser_reset();
|
||||
g_string_append_len(stream, buf, read_size);
|
||||
memset(buf, 0, sizeof(buf));
|
||||
if (g_str_has_suffix(stream->str, END_STREAM)) {
|
||||
@@ -107,7 +107,12 @@ debug_client(XMPPClient *client)
|
||||
|
||||
void connection_handler(XMPPClient *client)
|
||||
{
|
||||
int res = listen_for(client, STREAM_REQ);
|
||||
int res = listen_for(client, XML_START);
|
||||
if (res == -1) {
|
||||
return;
|
||||
}
|
||||
|
||||
res = listen_for(client, STREAM_REQ);
|
||||
if (res == -1) {
|
||||
return;
|
||||
}
|
||||
@@ -196,6 +201,9 @@ int main(int argc , char *argv[])
|
||||
perror("Listen failed");
|
||||
return 0;
|
||||
}
|
||||
|
||||
parser_init();
|
||||
|
||||
puts("Waiting for incoming connections...");
|
||||
|
||||
// connection accept
|
||||
@@ -211,6 +219,8 @@ int main(int argc , char *argv[])
|
||||
|
||||
connection_handler(client);
|
||||
|
||||
parser_close();
|
||||
|
||||
while (recv(listen_socket, NULL, 1, 0) > 0) {}
|
||||
shutdown(listen_socket, 2);
|
||||
close(listen_socket);
|
||||
|
||||
Reference in New Issue
Block a user