Commit da511ba4 authored by ROOL's avatar ROOL 🤖
Browse files

Review edits

* Add missing start.h header wanted by module.c
* Use a define HTTP_OTHER_METHODS instead of several 128 constants
* Report an error if reserved flags are set
* Report an error if malloc fails
* Add the exported SWIs to the assembler header too
* Minor comment style/indentation tidying
parent 1afc5049
......@@ -10,4 +10,4 @@ E08:Unable to parse URL
E09:Remote proxy not found. Please check the proxy settings
E0a:Security support module not present
E0b:Invalid parameter passed to SWI
E0c:Not enough internal resources to handle SWI successfully
E0c:Insufficient internal resources to handle SWI
......@@ -36,6 +36,7 @@
#include "dns.h"
#include "config.h"
#include "security.h"
#include "start.h"
#include "Httperror.h"
static volatile int callback_pending_flag = 0;
......@@ -280,7 +281,7 @@ _kernel_oserror *module_kill(int fatal, int podule, void *pw)
ses_kill_all();
session_init();
http_start_cleanup();
http_start_exit();
hosttrack_exit();
connpool_exit();
http_free_agent();
......
......@@ -43,16 +43,16 @@
#define HTTP_OTHER_METHOD_FLAG_TYPE_USER 0
#define HTTP_OTHER_METHOD_FLAG_TYPE_POST 1
#define HTTP_OTHER_METHOD_FLAG_TYPE_GET 2
#define HTTP_OTHER_METHOD_FLAG_TYPE_GET 2
#define HTTP_OTHER_METHOD_FLAG_MASK_TYPE 3
typedef struct {
char *name;
unsigned long flags;
unsigned long usage_counter;
char *name;
unsigned long flags;
unsigned long usage_counter;
} http_other_method;
http_other_method http_other_methods[128];
#define HTTP_OTHER_METHODS 128 /* Methods 0 & 255 reserved, 1-127 for us, 128-254 for the user */
static http_other_method http_other_methods[HTTP_OTHER_METHODS];
static int http_generate_request(Session *ses, _kernel_swi_regs *r);
static Session *http_new_session(_kernel_swi_regs *r);
......@@ -64,135 +64,135 @@ static _kernel_oserror *http_start_main(_kernel_swi_regs *r);
/* Called at start of module */
void http_start_init(void)
{
int i;
for (i=0; i < 128; i++) {
http_other_methods[i].name=NULL;
http_other_methods[i].flags=0;
http_other_methods[i].usage_counter=0;
}
/* Mark reserved methods as used */
http_other_methods[0].usage_counter=1;
/* Mark HTTP standard methods as used */
http_other_methods[method_HTTP_GET].usage_counter=1;
http_other_methods[method_HTTP_HEAD].usage_counter=1;
http_other_methods[method_HTTP_POST].usage_counter=1;
http_other_methods[method_HTTP_PUT].usage_counter=1;
http_other_methods[method_HTTP_OPTIONS].usage_counter=1;
http_other_methods[method_HTTP_TRACE].usage_counter=1;
http_other_methods[method_HTTP_DELETE].usage_counter=1;
int i;
for (i = 0; i < HTTP_OTHER_METHODS; i++) {
http_other_methods[i].name = NULL;
http_other_methods[i].flags = 0;
http_other_methods[i].usage_counter = 0;
}
/* Mark reserved methods as used */
http_other_methods[0].usage_counter = 1;
/* Mark HTTP standard methods as used */
http_other_methods[method_HTTP_GET].usage_counter = 1;
http_other_methods[method_HTTP_HEAD].usage_counter = 1;
http_other_methods[method_HTTP_POST].usage_counter = 1;
http_other_methods[method_HTTP_PUT].usage_counter = 1;
http_other_methods[method_HTTP_OPTIONS].usage_counter = 1;
http_other_methods[method_HTTP_TRACE].usage_counter = 1;
http_other_methods[method_HTTP_DELETE].usage_counter = 1;
}
/* Called at finalization of module */
void http_start_cleanup(void)
/* Called at finalisation of module */
void http_start_exit(void)
{
int i;
for (i=0; i < 128; i++) {
if (http_other_methods[i].name != NULL) {
free(http_other_methods[i].name);
http_other_methods[i].name=NULL;
}
http_other_methods[i].flags=0;
http_other_methods[i].usage_counter=0;
}
int i;
for (i = 0; i < HTTP_OTHER_METHODS; i++) {
if (http_other_methods[i].name != NULL) {
free(http_other_methods[i].name);
http_other_methods[i].name = NULL;
}
http_other_methods[i].flags = 0;
http_other_methods[i].usage_counter = 0;
}
}
/*************************************************************/
/* _kernel_oserror http_register_method (_kernel_swi_regs *r) */
/* _kernel_oserror http_register_method(_kernel_swi_regs *r) */
/*************************************************************/
/*************************************************************/
/* Must be called by application to register a certain non */
/* standard HTTP method. The values in the registers are: */
/* r0 = Pointer to 0 delimited HTTP method name */
/* r1 = Method flags */
/* r1 = Method flags */
/* On exit: */
/* r2 = method to use or -1 in case that there is no */
/* method number available or parameters are invalid */
/* r2 = method to use */
/*************************************************************/
_kernel_oserror *http_register_method(_kernel_swi_regs *r)
{
int i;
int first_unused_method;
int string_length;
/* Warning: Should not run in parallel!
But how to ensure this at multicore environment? */
/* Indicates failure. */
r->r[2]=-1;
if (r->r[0] == NULL) return make_error(HTTP_BAD_PARAMETER, 0);
first_unused_method=-1;
i=0;
while (i < 128) {
if (first_unused_method == -1 &&
http_other_methods[i].usage_counter == 0) {
/* Note first unused method in case that the requested
method doesn't already exists */
first_unused_method=i;
}
else if (http_other_methods[i].usage_counter > 0 &&
http_other_methods[i].name != NULL &&
http_other_methods[i].flags == (unsigned long) r->r[1] &&
strcmp(http_other_methods[i].name, (char *) r->r[0]) == 0) {
/* Method already exists */
/* And is used now by an additional application */
http_other_methods[i].usage_counter++;
r->r[2]=i;
return NULL;
}
i++;
}
if (first_unused_method == -1) return make_error(HTTP_NO_RESOURCES, 0);
/* Method doesn't exists yet and there is a free method we can use for it. */
string_length=strlen((char *) r->r[0]);
if (string_length > 0) {
if ((http_other_methods[first_unused_method].name=malloc(string_length + 1)) != NULL) {
http_other_methods[first_unused_method].usage_counter=1;
strcpy(http_other_methods[first_unused_method].name, (char *) r->r[0]);
http_other_methods[first_unused_method].flags=(unsigned long) r->r[1];
r->r[2]=first_unused_method;
}
}
return NULL;
int i = 0;
int first_unused_method, len;
char *buffer;
if (r->r[0] == NULL || (r->r[1] & ~HTTP_OTHER_METHOD_FLAG_MASK_TYPE)) {
return make_error(HTTP_BAD_PARAMETER, 0);
}
len = strlen((const char *)r->r[0]);
if (len == 0) return make_error(HTTP_BAD_PARAMETER, 0);
first_unused_method = -1;
while (i < HTTP_OTHER_METHODS) {
if (first_unused_method == -1 &&
http_other_methods[i].usage_counter == 0) {
/* Note first unused method in case the requested
* method doesn't already exist
*/
first_unused_method = i;
}
else {
if (http_other_methods[i].usage_counter > 0 &&
http_other_methods[i].name != NULL &&
http_other_methods[i].flags == (unsigned long)r->r[1] &&
strcmp(http_other_methods[i].name, (const char *)r->r[0]) == 0) {
/* Method already exists and is now used by
* an additional application
*/
http_other_methods[i].usage_counter++;
r->r[2] = i;
return NULL;
}
}
i++;
}
if (first_unused_method == -1) return make_error(HTTP_NO_RESOURCES, 0);
/* Method doesn't exist yet and there is a free method we can use for it */
buffer = malloc(len + 1);
if (buffer == NULL) return make_error(HTTP_NO_RESOURCES, 0);
http_other_methods[first_unused_method].name = buffer;
strcpy(buffer, (const char *)r->r[0]);
http_other_methods[first_unused_method].usage_counter = 1;
http_other_methods[first_unused_method].flags = (unsigned long)r->r[1];
r->r[2] = first_unused_method;
return NULL;
}
/*************************************************************/
/* _kernel_oserror http_deregister_method (_kernel_swi_regs *r) */
/*************************************************************/
/*************************************************************/
/* Must be called by application to deregister a certain non */
/* standard HTTP method previously registered with */
/* http_register_method. The values in the registers are: */
/* r0 = Pointer to 0 delimited HTTP method name */
/* r1 = Method Flags */
/*************************************************************/
/***************************************************************/
/* _kernel_oserror http_deregister_method(_kernel_swi_regs *r) */
/***************************************************************/
/***************************************************************/
/* Must be called by application to deregister a certain non */
/* standard HTTP method previously registered with */
/* http_register_method. The values in the registers are: */
/* r0 = Pointer to 0 delimited HTTP method name */
/* r1 = Method Flags */
/***************************************************************/
_kernel_oserror *http_deregister_method(_kernel_swi_regs *r)
{
int i;
/* Warning: Should not run in parallel!
But how to ensure this at multicore environment? */
if (r->r[0] == NULL) return make_error(HTTP_BAD_PARAMETER,0);
i=0;
while (i < 128) {
if (http_other_methods[i].usage_counter > 0 &&
http_other_methods[i].name != NULL &&
http_other_methods[i].flags == r->r[1] &&
strcmp(http_other_methods[i].name, (char *) r->r[0]) == 0) {
http_other_methods[i].usage_counter--;
if (http_other_methods[i].usage_counter == 0) {
/* Can be used for other methods now. */
if (http_other_methods[i].name != NULL) {
free(http_other_methods[i].name);
http_other_methods[i].name=NULL;
}
http_other_methods[i].flags=0;
}
return NULL;
}
i++;
}
return make_error(HTTP_METHOD_UNSUPPORTED,0);
int i = 0;
if (r->r[0] == NULL) return make_error(HTTP_BAD_PARAMETER,0);
while (i < HTTP_OTHER_METHODS) {
if (http_other_methods[i].usage_counter > 0 &&
http_other_methods[i].name != NULL &&
http_other_methods[i].flags == r->r[1] &&
strcmp(http_other_methods[i].name, (const char *)r->r[0]) == 0) {
/* Match found, decrement usage */
http_other_methods[i].usage_counter--;
if (http_other_methods[i].usage_counter == 0) {
/* Can be used for other methods now. */
free(http_other_methods[i].name);
http_other_methods[i].name = NULL;
http_other_methods[i].flags = 0;
}
return NULL;
}
i++;
}
return make_error(HTTP_METHOD_UNSUPPORTED,0);
}
/*************************************************************/
......@@ -538,8 +538,8 @@ static const char *http_method_text(const int method)
case method_HTTP_TRACE: return "TRACE";
case method_HTTP_DELETE: return "DELETE";
default: {
if (method < 128) return http_other_methods[method].name;
else return NULL;
if (method < HTTP_OTHER_METHODS) return http_other_methods[method].name;
return NULL;
}
}
}
......@@ -575,7 +575,7 @@ static int http_validate_user_supplied_data(Session *ses)
if (ses->method == method_HTTP_GET || ses->method == method_HTTP_HEAD ||
(ses->method < 128 &&
(ses->method < HTTP_OTHER_METHODS &&
(http_other_methods[ses->method].flags & HTTP_OTHER_METHOD_FLAG_MASK_TYPE) == HTTP_OTHER_METHOD_FLAG_TYPE_GET)) {
/* ... and add any other HTTP methods that MUST NOT include an entity
* body along with the request
......@@ -619,7 +619,7 @@ static int http_validate_user_supplied_data(Session *ses)
}
if (ses->method == method_HTTP_PUT || ses->method == method_HTTP_POST ||
(ses->method < 128 &&
(ses->method < HTTP_OTHER_METHODS &&
(http_other_methods[ses->method].flags & HTTP_OTHER_METHOD_FLAG_MASK_TYPE) == HTTP_OTHER_METHOD_FLAG_TYPE_POST)) {
/* ... and add any other HTTP methods that MUST include an entity
* body along with the request
......
......@@ -12,8 +12,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* HTTP (h.Httperror)
/*
* HTTP (h.Httperror)
*
*/
......@@ -38,5 +38,5 @@ typedef enum {
HTTP_PROXY_NOT_FOUND = HTTP_ERROR_NUM + 9, /* Unable to contact the proxy */
HTTP_NO_SECURITY = HTTP_ERROR_NUM + 10, /* No SSL support present */
HTTP_BAD_PARAMETER = HTTP_ERROR_NUM + 11, /* Invalid parameter(s) passed to SWI */
HTTP_NO_RESOURCES = HTTP_ERROR_NUM + 12 /* Not enough internal resources to handle SWI */
HTTP_NO_RESOURCES = HTTP_ERROR_NUM + 12 /* Insufficient internal resources to handle SWI */
} http_internal_error_codes;
......@@ -12,13 +12,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* HTTP (h.start)
/*
* HTTP (h.start)
*
*/
extern void http_start_init(void);
extern void http_start_cleanup(void);
extern void http_start_exit(void);
extern _kernel_oserror *http_register_method(_kernel_swi_regs *r);
extern _kernel_oserror *http_deregister_method(_kernel_swi_regs *r);
......@@ -46,8 +46,10 @@ SWIClass SETS AcornHTTPSWI_Name
AddSWI SecureReadData
AddSWI SecureStop
^ AcornHTTPSWI_Base + &3D
^ AcornHTTPSWI_Base + &3B
AddSWI RegisterMethod
AddSWI DeregisterMethod
AddSWI AddCookie
AddSWI ConsumeCookie
AddSWI EnumerateCookies
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment