int xhttp_rpc_build_page(rpc_ctx_t *ctx) { char *p; char *buf = ctx->reply.buf.s; int max_page_len = ctx->reply.buf.len; if (ctx->reply.body.len==0) if (0!=xhttp_rpc_build_content(ctx, NULL, NULL)) return -1; p = ctx->reply.body.s + ctx->reply.body.len; if (ctx->arg_received) { XHTTP_RPC_COPY_5(p,XHTTP_RPC_CODE_2, XHTTP_RPC_Response_Menu_Cmd_td_4d, XHTTP_RPC_Response_Menu_Cmd_tr_2, XHTTP_RPC_Response_Menu_Cmd_Table_2, XHTTP_RPC_Response_Foot); ctx->reply.body.len = p - ctx->reply.body.s; } return 0; error: LM_ERR("buffer 2 small\n"); ctx->reply.body.len = p - ctx->reply.body.s; return -1; }
/** Converts the variables provided in parameter ap according to formatting * string provided in parameter fmt into HTML format. * * This function takes the parameters provided in ap parameter and creates * HTML formatted parameters that will be put in the html document. * The format of input parameters is described in formatting string * fmt which follows the syntax of the management API. In the case of * an error the function will generate an error reply in err_reply parameter * instead. * @param ctx An error reply document will be generated here if the * function encounters a problem while processing input * parameters. * @param fmt Formatting string of the management API. * @param ap A pointer to the array of input parameters. * */ static int print_value(rpc_ctx_t* ctx, char fmt, va_list* ap, str *id) { str body; str *sp; char buf[PRINT_VALUE_BUF_LEN]; time_t dt; struct tm* t; switch(fmt) { case 'd': body.s = sint2str(va_arg(*ap, int), &body.len); break; case 'f': body.s = buf; body.len = snprintf(buf, PRINT_VALUE_BUF_LEN, "%f", va_arg(*ap, double)); if (body.len < 0) { LM_ERR("Error while converting double\n"); return -1; } break; case 'b': body.len = 1; body.s = ((va_arg(*ap, int)==0)?"0":"1"); break; case 't': body.s = buf; body.len = sizeof("19980717T14:08:55") - 1; dt = va_arg(*ap, time_t); t = gmtime(&dt); if (strftime(buf, PRINT_VALUE_BUF_LEN, "%Y%m%dT%H:%M:%S", t) == 0) { LM_ERR("Error while converting time\n"); return -1; } break; case 's': body.s = va_arg(*ap, char*); body.len = strlen(body.s); break; case 'S': sp = va_arg(*ap, str*); body = *sp; break; default: body.len = 0; body.s = NULL; LM_ERR("Invalid formatting character [%c]\n", fmt); return -1; } if (0!=xhttp_rpc_build_content(ctx, &body, id)) { rpc_fault(ctx, 500, "Internal Server Error"); return -1; } return 0; }
/** Implementation of rpc_add function required by the management API. * * This function will be called when an RPC management function calls * rpc->add to add a parameter to the xhttp_rpc reply being generated. */ static int rpc_add(rpc_ctx_t* ctx, char* fmt, ...) { void **void_ptr; struct rpc_data_struct *ds; va_list ap; if (0!=xhttp_rpc_build_content(ctx, NULL, NULL)) { rpc_fault(ctx, 500, "Internal Server Error"); return -1; } va_start(ap, fmt); while(*fmt) { if (*fmt == '{' || *fmt == '[') { void_ptr = va_arg(ap, void**); ds = new_data_struct(ctx); if (!ds) goto err; if (ctx->data_structs) free_data_struct(ctx->data_structs); ctx->data_structs = ds; *void_ptr = ds; } else { if (print_value(ctx, *fmt, &ap, NULL) < 0) goto err;