Skip to content

Commit

Permalink
Added onion_handler_opack, easy use of opack results. Fixed bug when …
Browse files Browse the repository at this point in the history
…sending data and break in the middle of send. oterm now uses internal resources instead of external, for !__DEBUG__. Added README.rst to oterm.

onion_handler_opack can be used to easy use opack results, only needs the opack writer and a path. Actually it can be used with your own writers, check onion_opack_render signature and do a funtion like that.

There was a bug when sending on SSL a big file, as it send max 16kB. Now it tries harder.

Opack now when compiled in non-debug mode returns the internal resources, that is, internal files, instead of using the directory handler.

The oterm README.rst file gives insight on how to use oterm, and where to find more information.
  • Loading branch information
davidmoreno committed Nov 6, 2010
1 parent b101d81 commit 000c0ee
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 42 deletions.
6 changes: 3 additions & 3 deletions examples/oterm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
add_definitions(-D__DEBUG__)
#add_definitions(-D__DEBUG__)

include_directories (${CMAKE_SOURCE_DIR}/src/onion/ ${CMAKE_SOURCE_DIR}/src/handlers/ ${CMAKE_SOURCE_DIR}/src/onionssl/)

add_custom_command(
OUTPUT oterm_data.c
COMMAND opack ${CMAKE_CURRENT_SOURCE_DIR}/index.html ${CMAKE_CURRENT_SOURCE_DIR}/oterm.js
${CMAKE_CURRENT_SOURCE_DIR}/oterm_input.js ${CMAKE_CURRENT_SOURCE_DIR}/oterm_data.js
${CMAKE_CURRENT_SOURCE_DIR}/jquery-1.4.3.min.js
${CMAKE_CURRENT_SOURCE_DIR}/oterm_parser.js ${CMAKE_CURRENT_SOURCE_DIR}/jquery-1.4.3.min.js
-o ${CMAKE_CURRENT_BINARY_DIR}/oterm_data.c
DEPENDS opack ${CMAKE_CURRENT_SOURCE_DIR}/index.html ${CMAKE_CURRENT_SOURCE_DIR}/oterm.js
${CMAKE_CURRENT_SOURCE_DIR}/oterm_input.js ${CMAKE_CURRENT_SOURCE_DIR}/oterm_data.js
${CMAKE_CURRENT_SOURCE_DIR}/jquery-1.4.3.min.js
${CMAKE_CURRENT_SOURCE_DIR}/oterm_parser.js ${CMAKE_CURRENT_SOURCE_DIR}/jquery-1.4.3.min.js
)

add_executable(oterm oterm.c oterm_handler.c oterm_data.c)
Expand Down
45 changes: 45 additions & 0 deletions examples/oterm/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
Onion web-based GNU/Linux terminal
==================================

With Oterm you can acces with a standard compilant browser to a remote terminal,
using SSL and authenticated.

It has a server component, that is as light as possible, that is the container of
the processes, and redirects all inforamtion to the web based terminal.

The terminal uses HTML/Javascript to emulate all the terminal commands and
control characters, and serves all that as if it were a web page.

Usage
-----

You need the libonion http web server library, and compile it all. To execute it
just run the command oterm, and connect to the given address.

More help is provided with --help.

Current features
----------------

* Understand most linux terminal commands, and allow to use many terminal based
programs as htop, atop, mc, vi, emacs

Planned features
----------------

* Make it behave like a screen for web, allowing to create new sessions, recover
old ones...
* Make it multiuser, each with its own sessions.
* Make it RESTfull, now it does polling, so its not as efficient as could be.
* More terminal functionalities: It does not do yet all that a xterm do, for example
it does not resize yet.
* Fix bugs.

Contact
-------

There is a github page at https://github.com/davidmoreno/onion, where you can fill new
issues and feature request. Also you can send emails to dmoreno (works at) coralbits.com.

Last if you need prefesional guidance about oterm or onion, you can contact the upper
email address to ask for more information about consulting.
21 changes: 18 additions & 3 deletions examples/oterm/oterm.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,21 @@
#include <onion_handler_path.h>
#include <onion_handler_static.h>
#include <onion_handler_auth_pam.h>
#include <onion_handler_opack.h>

#ifdef __DEBUG__
#include <onion_handler_directory.h>
#endif

#include "oterm_handler.h"
#include <onion_handler_directory.h>


void opack_index_html(onion_response *res);
void opack_oterm_js(onion_response *res);
void opack_oterm_input_js(onion_response *res);
void opack_oterm_data_js(onion_response *res);
void opack_oterm_parser_js(onion_response *res);
void opack_jquery_1_4_3_min_js(onion_response *res);

onion_ssl *o=NULL;

Expand Down Expand Up @@ -95,8 +106,12 @@ int main(int argc, char **argv){
#ifdef __DEBUG__
onion_handler *dir=onion_handler_directory(".");
#else
onion_handler *dir=onion_handler_path("^/$",oterm_handler_index());
onion_handler_add(dir, onion_handler_path("^/oterm.js$",oterm_handler_oterm()));
onion_handler *dir=onion_handler_opack("/",opack_index_html);
onion_handler_add(dir, onion_handler_opack("/jquery-1.4.3.min.js",opack_jquery_1_4_3_min_js));
onion_handler_add(dir, onion_handler_opack("/oterm.js",opack_oterm_js));
onion_handler_add(dir, onion_handler_opack("/oterm_input.js",opack_oterm_input_js));
onion_handler_add(dir, onion_handler_opack("/oterm_parser.js",opack_oterm_parser_js));
onion_handler_add(dir, onion_handler_opack("/oterm_data.js",opack_oterm_data_js));
#endif

onion_handler_add(dir, onion_handler_path("^/term$",oterm_handler_data()));
Expand Down
31 changes: 0 additions & 31 deletions examples/oterm/oterm_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,6 @@
#include <onion_response.h>
#include <onion_handler.h>

void opack_index_html(onion_response *res);
void opack_oterm_js(onion_response *res);
void opack_oterm_input_js(onion_response *res);
void opack_oterm_data_js(onion_response *res);
void opack_jquery_1_4_3_min_js(onion_response *res);

int oterm_index(void *d, onion_request *req){
onion_response *res=onion_response_new(req);
onion_response_write_headers(res);
opack_index_html(res);
return 1;
}
int oterm_oterm(void *d, onion_request *req){
onion_response *res=onion_response_new(req);
onion_response_set_header(res,"Content-Type","text/javascript");
onion_response_write_headers(res);

opack_jquery_1_4_3_min_js(res);
opack_oterm_js(res);
opack_oterm_input_js(res);
opack_oterm_data_js(res);

return 1;
}
onion_handler *oterm_handler_index(){
return onion_handler_new((onion_handler_handler)oterm_index, NULL, NULL);
}
onion_handler *oterm_handler_oterm(){
return onion_handler_new((onion_handler_handler)oterm_oterm, NULL, NULL);
}


typedef struct{
int fd;
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
include_directories (${CMAKE_SOURCE_DIR}/src/onion/)

add_library(onion_handlers STATIC onion_handler_static.c onion_handler_path.c onion_handler_directory.c
onion_handler_regexp.c onion_handler_auth_pam.c)
onion_handler_regexp.c onion_handler_auth_pam.c onion_handler_opack.c)

target_link_libraries(onion_handlers pam pam_misc)
70 changes: 70 additions & 0 deletions src/handlers/onion_handler_opack.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Onion HTTP server library
Copyright (C) 2010 David Moreno Montero
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <string.h>
#include <malloc.h>
#include <unistd.h>
#include <regex.h>

#include <onion_handler.h>
#include <onion_response.h>

#include "onion_handler_opack.h"

struct onion_handler_opack_data_t{
char *path;
onion_opack_renderer render;
};

typedef struct onion_handler_opack_data_t onion_handler_opack_data;

int onion_handler_opack_handler(onion_handler_opack_data *d, onion_request *request){
if (strcmp(d->path, onion_request_get_path(request))!=0)
return 0;

onion_response *res=onion_response_new(request);
onion_response_write_headers(res);

d->render(res);

onion_response_free(res);
return 1;
}


void onion_handler_opack_delete(onion_handler_opack_data *data){
free(data->path);
free(data);
}

/**
* @short Creates an path handler. If the path matches the regex, it reomves that from the regexp and goes to the inside_level.
*
* If on the inside level nobody answers, it just returns NULL, so ->next can answer.
*/
onion_handler *onion_handler_opack(const char *path, onion_opack_renderer render){
onion_handler_opack_data *priv_data=malloc(sizeof(onion_handler_opack_data));

priv_data->path=strdup(path);
priv_data->render=render;

onion_handler *ret=onion_handler_new((onion_handler_handler)onion_handler_opack_handler,
priv_data, (onion_handler_private_data_free) onion_handler_opack_delete);
return ret;
}

37 changes: 37 additions & 0 deletions src/handlers/onion_handler_opack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Onion HTTP server library
Copyright (C) 2010 David Moreno Montero
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __ONION_HANDLER_OPACK__
#define __ONION_HANDLER_OPACK__

#include <onion_types.h>

#ifdef __cplusplus
extern "C"{
#endif

typedef void (*onion_opack_renderer)(onion_response *res);

/// Creates an path handler. If the path matches the regex, it reomves that from the regexp and goes to the inside_level.
onion_handler *onion_handler_opack(const char *path, onion_opack_renderer opack);

#ifdef __cplusplus
}
#endif

#endif
13 changes: 11 additions & 2 deletions src/onion/onion_response.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <unistd.h>
#include <stdio.h>
#include <stdarg.h>
//#include <time.h>
#include <libgen.h>

#include "onion_dict.h"
#include "onion_request.h"
Expand Down Expand Up @@ -97,7 +97,16 @@ void onion_response_write_headers(onion_response *res){
int onion_response_write(onion_response *res, const char *data, unsigned int length){
void *fd=onion_response_get_socket(res);
onion_write write=onion_response_get_writer(res);
int w=write(fd, data, length);
int w;
int pos=0;
while ( (w=write(fd, &data[pos], length)) != length){
if (w==0){
fprintf(stderr,"%s:%d Error writing. Maybe closed connection.\n",basename(__FILE__),__LINE__);
break;
}
pos+=w;
length-=w;
}
res->sent_bytes+=length;
return w;
}
Expand Down
6 changes: 4 additions & 2 deletions tools/opack/opack.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,13 @@ void parse_file(const char *filename, FILE *outfd){
exit(3);
}
char *fname=funcname(basename((char*)filename));
char buffer[1024];
char buffer[4097];
buffer[4096]=0;

fprintf(stderr, "Parsing: %s to 'int %s(onion_response *res);'.\n",filename, fname);
fprintf(outfd,"int %s(onion_response *res){\n char data[]={\n",fname);
int r, i;
while ( (r=fread(buffer,1,sizeof(buffer),fd)) !=0 ){
while ( (r=fread(buffer,1,sizeof(buffer)-1,fd)) !=0 ){
for (i=0;i<r;i++){
fprintf(outfd,"0x%02X, ", buffer[i]&0x0FF);
}
Expand All @@ -128,3 +129,4 @@ void print_help(const char *name){
fprintf(stderr,"This way this function is very easily used from onion handlers as needed.\n");
exit(1);
}

0 comments on commit 000c0ee

Please sign in to comment.