Adaptive Framework  0.9.0
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
afw_request_handler_director.c
Go to the documentation of this file.
1 // See the 'COPYING' file in the project root for licensing information.
2 /*
3  * Implementation of afw_request_handler interface for director
4  *
5  * Copyright (c) 2010-2023 Clemson University
6  *
7  */
8 
14 #include "afw_internal.h"
15 
16 
17 
18 /* Declares and rti/inf defines for interface afw_request_handler */
19 #define AFW_IMPLEMENTATION_ID "director"
21 
22 
25  const afw_pool_t *p;
26  const afw_object_t *properties;
29 
30 
31 
34  const afw_server_t *server,
35  const afw_utf8_z_t *config_file,
36  const afw_pool_t *p, afw_xctx_t *xctx)
37 {
39 
40  /* Allocate memory for self. */
42 
43  /* Initialize Self. */
44  self->pub.inf = &impl_afw_request_handler_inf;
45  self->p = p;
46 
47  /* Get handler head. */
48  self->head = afw_environment_get_singleton(
49  &AFW_REQUEST_HANDLER_S_SINGLETON_KEY_HEAD,
50  xctx);
51  if (!self->head) {
52  AFW_THROW_ERROR_Z(general, "Request handler head missing.", xctx);
53  }
54 
55  /* Return new adaptor. */
56  return (const afw_request_handler_t *)self;
57 }
58 
59 
60 
61 /*
62  * Implementation of method release of interface afw_request_handler.
63  */
64 void
65 impl_afw_request_handler_release(
66  const afw_request_handler_t * instance,
67  afw_xctx_t *xctx)
68 {
69  /*
70  * Continue release, even if there is already an error. Don't overwrite
71  * existing error.
72  */
73 
74  /* Resources released when execution context (xctx) is released. */
75 }
76 
77 
78 
79 /*
80  * Implementation of method process of interface afw_request_handler.
81  */
82 void
84  const afw_request_handler_t * instance,
85  const afw_request_t * request,
86  afw_xctx_t *xctx)
87 {
90  const afw_request_handler_entry_t *request_handler_entry;
91 
92  /* Call appropriate handler. */
93  for (request_handler_entry = self->head->first_handler; request_handler_entry;
94  request_handler_entry = request_handler_entry->next)
95  {
96  if (afw_utf8_starts_with(request->uri,
97  request_handler_entry->uri_prefix))
98  {
99  afw_request_handler_process(request_handler_entry->rh, request, xctx);
100  return;
101  }
102  }
103 
104  AFW_THROW_ERROR_FZ(not_found, xctx,
105  "Prefix %" AFW_UTF8_FMT " not found",
106  AFW_UTF8_FMT_ARG(request->uri));
107 }
AFW_DEFINE(const afw_object_t *)
Adaptive Framework Core Internal.
Interface afw_interface implementation declares.
#define AFW_UTF8_FMT_ARG(A_STRING)
Convenience Macro for use with AFW_UTF8_FMT to specify arg.
Definition: afw_common.h:605
#define AFW_UTF8_FMT
Format string specifier used for afw_utf8_t.
Definition: afw_common.h:588
afw_utf8_octet_t afw_utf8_z_t
NFC normalized UTF-8 null terminated string.
Definition: afw_common.h:523
void * afw_environment_get_singleton(const afw_utf8_t *singleton_key, afw_xctx_t *xctx)
Get the singleton value.
#define AFW_THROW_ERROR_FZ(code, xctx, format_z,...)
Macro used to set error and 0 rv in xctx and throw it.
Definition: afw_error.h:319
#define AFW_THROW_ERROR_Z(code, message_z, xctx)
Macro used to set error and 0 rv in xctx and throw it.
Definition: afw_error.h:283
#define afw_pool_calloc_type(instance, type, xctx)
Macro to allocate cleared memory to hold type in pool.
Definition: afw_pool.h:167
void impl_afw_request_handler_process(const afw_request_handler_t *instance, const afw_request_t *request, afw_xctx_t *xctx)
#define afw_request_handler_process(instance, request, xctx)
Call method process of interface afw_request_handler.
afw_request_handler_director_create(const afw_server_t *server, const afw_utf8_z_t *config_file, const afw_pool_t *p, afw_xctx_t *xctx)
Create a request handler for director.
afw_boolean_t afw_utf8_starts_with(const afw_utf8_t *string, const afw_utf8_t *starts_with)
Check to see if a string starts with another string.
Interface afw_object public struct.
Interface afw_pool public struct.
Interface afw_request_handler public struct.
Interface afw_request public struct.
Interface afw_server public struct.
Interface afw_xctx public struct.