1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
|
/* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <apr_lib.h>
#include <apr_date.h>
#include <apr_time.h>
#include <apr_strings.h>
#include "md.h"
#include "md_json.h"
#include "md_log.h"
#include "md_result.h"
static const char *dup_trim(apr_pool_t *p, const char *s)
{
char *d = apr_pstrdup(p, s);
if (d) apr_collapse_spaces(d, d);
return d;
}
md_result_t *md_result_make(apr_pool_t *p, apr_status_t status)
{
md_result_t *result;
result = apr_pcalloc(p, sizeof(*result));
result->p = p;
result->md_name = MD_OTHER;
result->status = status;
return result;
}
md_result_t *md_result_md_make(apr_pool_t *p, const char *md_name)
{
md_result_t *result = md_result_make(p, APR_SUCCESS);
result->md_name = md_name;
return result;
}
void md_result_reset(md_result_t *result)
{
apr_pool_t *p = result->p;
memset(result, 0, sizeof(*result));
result->p = p;
}
static void on_change(md_result_t *result)
{
if (result->on_change) result->on_change(result, result->on_change_data);
}
void md_result_activity_set(md_result_t *result, const char *activity)
{
md_result_activity_setn(result, activity? apr_pstrdup(result->p, activity) : NULL);
}
void md_result_activity_setn(md_result_t *result, const char *activity)
{
result->activity = activity;
result->problem = result->detail = NULL;
result->subproblems = NULL;
on_change(result);
}
void md_result_activity_printf(md_result_t *result, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
md_result_activity_setn(result, apr_pvsprintf(result->p, fmt, ap));
va_end(ap);
}
void md_result_set(md_result_t *result, apr_status_t status, const char *detail)
{
result->status = status;
result->problem = NULL;
result->detail = detail? apr_pstrdup(result->p, detail) : NULL;
result->subproblems = NULL;
on_change(result);
}
void md_result_problem_set(md_result_t *result, apr_status_t status,
const char *problem, const char *detail,
const md_json_t *subproblems)
{
result->status = status;
result->problem = dup_trim(result->p, problem);
result->detail = apr_pstrdup(result->p, detail);
result->subproblems = subproblems? md_json_clone(result->p, subproblems) : NULL;
on_change(result);
}
void md_result_problem_printf(md_result_t *result, apr_status_t status,
const char *problem, const char *fmt, ...)
{
va_list ap;
result->status = status;
result->problem = dup_trim(result->p, problem);
va_start(ap, fmt);
result->detail = apr_pvsprintf(result->p, fmt, ap);
va_end(ap);
result->subproblems = NULL;
on_change(result);
}
void md_result_printf(md_result_t *result, apr_status_t status, const char *fmt, ...)
{
va_list ap;
result->status = status;
va_start(ap, fmt);
result->detail = apr_pvsprintf(result->p, fmt, ap);
va_end(ap);
result->subproblems = NULL;
on_change(result);
}
void md_result_delay_set(md_result_t *result, apr_time_t ready_at)
{
result->ready_at = ready_at;
on_change(result);
}
md_result_t*md_result_from_json(const struct md_json_t *json, apr_pool_t *p)
{
md_result_t *result;
const char *s;
result = md_result_make(p, APR_SUCCESS);
result->status = (int)md_json_getl(json, MD_KEY_STATUS, NULL);
result->problem = md_json_dups(p, json, MD_KEY_PROBLEM, NULL);
result->detail = md_json_dups(p, json, MD_KEY_DETAIL, NULL);
result->activity = md_json_dups(p, json, MD_KEY_ACTIVITY, NULL);
s = md_json_dups(p, json, MD_KEY_VALID_FROM, NULL);
if (s && *s) result->ready_at = apr_date_parse_rfc(s);
result->subproblems = md_json_dupj(p, json, MD_KEY_SUBPROBLEMS, NULL);
return result;
}
struct md_json_t *md_result_to_json(const md_result_t *result, apr_pool_t *p)
{
md_json_t *json;
char ts[APR_RFC822_DATE_LEN];
json = md_json_create(p);
md_json_setl(result->status, json, MD_KEY_STATUS, NULL);
if (result->status > 0) {
char buffer[HUGE_STRING_LEN];
apr_strerror(result->status, buffer, sizeof(buffer));
md_json_sets(buffer, json, "status-description", NULL);
}
if (result->problem) md_json_sets(result->problem, json, MD_KEY_PROBLEM, NULL);
if (result->detail) md_json_sets(result->detail, json, MD_KEY_DETAIL, NULL);
if (result->activity) md_json_sets(result->activity, json, MD_KEY_ACTIVITY, NULL);
if (result->ready_at > 0) {
apr_rfc822_date(ts, result->ready_at);
md_json_sets(ts, json, MD_KEY_VALID_FROM, NULL);
}
if (result->subproblems) {
md_json_setj(result->subproblems, json, MD_KEY_SUBPROBLEMS, NULL);
}
return json;
}
static int str_cmp(const char *s1, const char *s2)
{
if (s1 == s2) return 0;
if (!s1) return -1;
if (!s2) return 1;
return strcmp(s1, s2);
}
int md_result_cmp(const md_result_t *r1, const md_result_t *r2)
{
int n;
if (r1 == r2) return 0;
if (!r1) return -1;
if (!r2) return 1;
if ((n = r1->status - r2->status)) return n;
if ((n = str_cmp(r1->problem, r2->problem))) return n;
if ((n = str_cmp(r1->detail, r2->detail))) return n;
if ((n = str_cmp(r1->activity, r2->activity))) return n;
return (int)(r1->ready_at - r2->ready_at);
}
void md_result_assign(md_result_t *dest, const md_result_t *src)
{
dest->status = src->status;
dest->problem = src->problem;
dest->detail = src->detail;
dest->activity = src->activity;
dest->ready_at = src->ready_at;
dest->subproblems = src->subproblems;
}
void md_result_dup(md_result_t *dest, const md_result_t *src)
{
dest->status = src->status;
dest->problem = src->problem? dup_trim(dest->p, src->problem) : NULL;
dest->detail = src->detail? apr_pstrdup(dest->p, src->detail) : NULL;
dest->activity = src->activity? apr_pstrdup(dest->p, src->activity) : NULL;
dest->ready_at = src->ready_at;
dest->subproblems = src->subproblems? md_json_clone(dest->p, src->subproblems) : NULL;
on_change(dest);
}
void md_result_log(md_result_t *result, unsigned int level)
{
if (md_log_is_level(result->p, (md_log_level_t)level)) {
const char *sep = "";
const char *msg = "";
if (result->md_name) {
msg = apr_psprintf(result->p, "md[%s]", result->md_name);
sep = " ";
}
if (result->activity) {
msg = apr_psprintf(result->p, "%s%swhile[%s]", msg, sep, result->activity);
sep = " ";
}
if (result->problem) {
msg = apr_psprintf(result->p, "%s%sproblem[%s]", msg, sep, result->problem);
sep = " ";
}
if (result->detail) {
msg = apr_psprintf(result->p, "%s%sdetail[%s]", msg, sep, result->detail);
sep = " ";
}
if (result->subproblems) {
msg = apr_psprintf(result->p, "%s%ssubproblems[%s]", msg, sep,
md_json_writep(result->subproblems, result->p, MD_JSON_FMT_COMPACT));
sep = " ";
}
md_log_perror(MD_LOG_MARK, (md_log_level_t)level, result->status, result->p, "%s", msg);
}
}
void md_result_on_change(md_result_t *result, md_result_change_cb *cb, void *data)
{
result->on_change = cb;
result->on_change_data = data;
}
apr_status_t md_result_raise(md_result_t *result, const char *event, apr_pool_t *p)
{
if (result->on_raise) return result->on_raise(result, result->on_raise_data, event, p);
return APR_SUCCESS;
}
void md_result_holler(md_result_t *result, const char *event, apr_pool_t *p)
{
if (result->on_holler) result->on_holler(result, result->on_holler_data, event, p);
}
void md_result_on_raise(md_result_t *result, md_result_raise_cb *cb, void *data)
{
result->on_raise = cb;
result->on_raise_data = data;
}
void md_result_on_holler(md_result_t *result, md_result_holler_cb *cb, void *data)
{
result->on_holler = cb;
result->on_holler_data = data;
}
|