int http_get(struct soap *soap) { char *s = strchr(soap->path, '?'); if (s && !strcmp(s, "?test")) { soap_response(soap, SOAP_HTML); soap_send(soap, "<HTML>You called test().</HTML>"); soap_end_send(soap); return SOAP_OK; } soap_response(soap, SOAP_HTML); soap_send(soap, "<HTML>success - afd is running</HTML>"); soap_end_send(soap); return SOAP_OK; }
static int out_element(struct soap *soap, const struct soap_dom_element *node, const char *prefix, const char *name) { if (node->head && soap_send(soap, node->head)) return soap->error; if (node->type && node->node) { if (prefix && *prefix) { char *s = (char*)SOAP_MALLOC(soap, strlen(prefix) + strlen(name) + 2); if (!s) return soap->error = SOAP_EOM; sprintf(s, "%s:%s", prefix, name); soap_putelement(soap, node->node, s, 0, node->type); SOAP_FREE(soap, s); } else return soap_putelement(soap, node->node, name, 0, node->type); } else if (prefix && *prefix) { char *s; if (strlen(prefix) + strlen(name) < sizeof(soap->msgbuf)) s = soap->msgbuf; else { s = (char*)SOAP_MALLOC(soap, strlen(prefix) + strlen(name) + 2); if (!s) return soap->error = SOAP_EOM; } sprintf(s, "%s:%s", prefix, name); if (soap_element(soap, s, 0, NULL)) /* element() */ return soap->error; if (s != soap->msgbuf) SOAP_FREE(soap, s); } else if (*name != '-') return soap_element(soap, name, 0, NULL); /* element() */ return soap->error; }
int main(int argc, char **argv) { char *buf; size_t len; struct soap soap; soap_init(&soap); /* chunking conserves memory and is generally faster: */ soap_set_omode(&soap, SOAP_IO_CHUNK); if (argc < 2) { /* CGI server */ struct http_post_handlers handlers[] = { { "image/jpg", jpg_handler }, { "image/*", image_handler }, { "image/*;*", image_handler }, { "text/*", text_handler }, { "text/*;*", text_handler }, { NULL } }; soap_register_plugin_arg(&soap, http_post, handlers); /* register plugin (server only) */ soap_serve(&soap); exit(0); } /* client */ if (soap_post_connect(&soap, argv[1], NULL, "text/html") || soap_send(&soap, "<html>") || soap_send(&soap, argc == 2 ? "Hello" : argv[2]) || soap_send(&soap, "</html>") || soap_end_send(&soap)) { soap_print_fault(&soap, stderr); exit(1); } /* after sending, receive body (note: POST handlers should not be set) */ if (soap_begin_recv(&soap) || soap_http_body(&soap, &buf, &len) || soap_end_recv(&soap)) { soap_print_fault(&soap, stderr); exit(1); } printf("Received %lu bytes of type %s:\n", (unsigned long)len, soap.http_content?soap.http_content:""); soap_end(&soap); soap_done(&soap); return 0; }
/* the image handler responds with HTTP OK and a text/html body */ int image_handler(struct soap *soap) { char *buf; size_t len; soap_http_body(soap, &buf, &len); soap_response(soap, SOAP_HTML); soap_send(soap, "<html>Image received</html>"); soap_end_send(soap); return SOAP_OK; }
static int out_element(struct soap *soap, const struct soap_dom_element *node, const char *prefix, const char *name) { if (node->head && soap_send(soap, node->head)) return soap->error; if (node->type && node->node) { if (prefix && *prefix) { size_t l = strlen(prefix) + strlen(name); char *s = (char*)SOAP_MALLOC(soap, l + 2); if (!s) return soap->error = SOAP_EOM; (SOAP_SNPRINTF(s, l + 2, l + 1), "%s:%s", prefix, name); soap_putelement(soap, node->node, s, 0, node->type); SOAP_FREE(soap, s); } else return soap_putelement(soap, node->node, name, 0, node->type); } else if (prefix && *prefix) { size_t l = strlen(prefix) + strlen(name); char *s; if (l + 1 < sizeof(soap->msgbuf)) s = soap->msgbuf; else { s = (char*)SOAP_MALLOC(soap, l + 2); if (!s) return soap->error = SOAP_EOM; } (SOAP_SNPRINTF(s, l + 2, l + 1), "%s:%s", prefix, name); soap_element(soap, s, 0, NULL); /* element() */ if (s != soap->msgbuf) SOAP_FREE(soap, s); } else if (*name != '-') { soap_mode m = soap->mode; if ((soap->mode & SOAP_DOM_ASIS)) soap->mode &= ~SOAP_XML_INDENT; soap_element(soap, name, 0, NULL); /* element() */ soap->mode = m; } return soap->error; }
SOAP_FMAC1 int SOAP_FMAC2 soap_out_xsd__anyType(struct soap *soap, const char *tag, int id, const struct soap_dom_element *node, const char *type) { if (node) { const char *prefix; /* namespace prefix, if namespace is present */ size_t colon; if (!(soap->mode & SOAP_DOM_ASIS)) { const struct soap_dom_attribute *att; for (att = node->atts; att; att = att->next) { if (att->name && att->data && !strncmp(att->name, "xmlns:", 6)) { if (soap_push_namespace(soap, att->name + 6, att->data) == NULL) return soap->error; } else if (att->name && att->data && !strcmp(att->name, "xmlns")) { if (soap_push_namespace(soap, "", att->data) == NULL) return soap->error; } } } if (node->name) tag = node->name; else if (!tag) tag = "-"; DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DOM node '%s' output at level %u\n", tag, soap->level)); if ((prefix = strchr(tag, ':'))) { colon = prefix - tag + 1; if (colon > sizeof(soap->tag)) colon = sizeof(soap->tag); } else colon = 0; prefix = NULL; if (node->nstr && *node->nstr && !(soap->mode & SOAP_DOM_ASIS)) { if (colon) { strncpy(soap->tag, tag, colon - 1); soap->tag[colon - 1] = '\0'; if ((prefix = soap_push_ns_prefix(soap, soap->tag, node->nstr, 1)) == NULL || out_element(soap, node, prefix, tag + colon)) return soap->error; } else { if ((prefix = soap_lookup_ns_prefix(soap, node->nstr))) { if (out_element(soap, node, prefix, tag + colon)) return soap->error; } else { if ((prefix = soap_push_ns_prefix(soap, NULL, node->nstr, 1)) == NULL || out_element(soap, node, prefix, tag + colon)) return soap->error; } } } else { colon = 0; if (out_element(soap, node, NULL, tag)) return soap->error; } if (!node->type || !node->node) { struct soap_dom_attribute *att; struct soap_dom_element *elt; for (att = node->atts; att; att = att->next) { if (att->name) { if (att->nstr && !(soap->mode & SOAP_DOM_ASIS)) { const char *p; if ((att->nstr == node->nstr || (node->nstr && !strcmp(att->nstr, node->nstr))) && prefix) { if (out_attribute(soap, prefix, att->name, att->data, att->wide, 0)) return soap->error; } else if ((p = soap_lookup_ns_prefix(soap, att->nstr))) { if (out_attribute(soap, p, att->name, att->data, att->wide, 0)) return soap->error; } else if (!strncmp(att->name, "xml", 3)) { if (out_attribute(soap, NULL, att->name, att->data, att->wide, 0)) return soap->error; } else if ((p = soap_push_ns_prefix(soap, NULL, att->nstr, 0)) == NULL || out_attribute(soap, p, att->name, att->data, att->wide, 0)) return soap->error; } else if (soap_attribute(soap, att->name, att->wide ? soap_wchar2s(soap, att->wide) : att->data)) return soap->error; } } if ((soap->mode & SOAP_DOM_ASIS) && !node->data && !node->wide && !node->elts && !node->tail) { if (*tag != '-' && soap_element_start_end_out(soap, tag)) return soap->error; } else { if (*tag != '-' && soap_element_start_end_out(soap, NULL)) return soap->error; if (node->data) { if (soap_string_out(soap, node->data, 0)) return soap->error; } else if (node->wide) { if (soap_wstring_out(soap, node->wide, 0)) return soap->error; } for (elt = node->elts; elt; elt = elt->next) { if (soap_out_xsd__anyType(soap, tag, 0, elt, NULL)) return soap->error; } if (node->tail && soap_send(soap, node->tail)) return soap->error; if (!prefix || !*prefix) { DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of DOM node '%s'\n", tag + colon)); if (soap_element_end_out(soap, tag + colon)) return soap->error; } else { char *s; if (strlen(prefix) + strlen(tag + colon) < sizeof(soap->msgbuf)) s = soap->msgbuf; else { s = (char*)SOAP_MALLOC(soap, strlen(prefix) + strlen(tag + colon) + 2); if (!s) return soap->error = SOAP_EOM; } DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of DOM node '%s'\n", tag)); sprintf(s, "%s:%s", prefix, tag + colon); soap_pop_namespace(soap); if (soap_element_end_out(soap, s)) return soap->error; if (s != soap->msgbuf) SOAP_FREE(soap, s); } } } } return SOAP_OK; }
SOAP_FMAC1 int SOAP_FMAC2 soap_out_xsd__anyType(struct soap *soap, const char *tag, int id, const struct soap_dom_element *node, const char *type) { if (node) { struct soap_dom_element *elt; struct soap_dom_attribute *att; register struct soap_ilist *p = NULL, *q; struct Namespace *ns = NULL; const char *prefix; /* namespace prefix, if namespace is present */ size_t colon = 0; if (node->name) tag = node->name; if (!tag) tag = "_"; if ((prefix = strchr(tag, ':'))) { colon = prefix - tag + 1; if (colon > sizeof(soap->tag)) colon = sizeof(soap->tag); } prefix = NULL; DBGLOG(TEST, SOAP_MESSAGE(fdebug, "DOM node '%s'\n", tag)); if (node->nstr) { if ((p = soap_lookup_ns(soap, node->nstr))) { prefix = p->id; p = NULL; if (out_element(soap, node, prefix, tag + colon, NULL)) return soap->error; } else if (colon) { strncpy(soap->tag, tag, colon - 1); soap->tag[colon - 1] = '\0'; if (!(p = soap_enter_ns(soap, soap->tag, node->nstr))) return soap->error = SOAP_EOM; prefix = p->id; if (out_element(soap, node, prefix, tag + colon, node->nstr)) return soap->error; } else { for (ns = soap->local_namespaces; ns && ns->id; ns++) if (ns->ns == node->nstr || !strcmp(ns->ns, node->nstr)) { /* if (soap->encodingStyle || ns == soap->local_namespaces) */ prefix = ns->id; if (out_element(soap, node, ns->id, tag + colon, NULL)) return soap->error; break; } if (!ns || !ns->id) { sprintf(soap->tag, SOAP_DOMID_FORMAT, soap->idnum++); if (!(p = soap_enter_ns(soap, soap->tag, node->nstr))) return soap->error = SOAP_EOM; prefix = p->id; if (out_element(soap, node, prefix, tag + colon, node->nstr)) return soap->error; } } } else if (out_element(soap, node, NULL, tag + colon, NULL)) return soap->error; if (node->type && node->node) return SOAP_OK; for (att = node->atts; att; att = att->next) { if (att->name) { if (att->nstr) { if ((att->nstr == node->nstr || (node->nstr && !strcmp(att->nstr, node->nstr))) && prefix) { if (out_attribute(soap, prefix, att->name, att->data)) return soap->error; } else if ((q = soap_lookup_ns(soap, att->nstr))) { if (out_attribute(soap, q->id, att->name, att->data)) return soap->error; } else { for (ns = soap->local_namespaces; ns && ns->id; ns++) { if (ns->ns == att->nstr || !strcmp(ns->ns, att->nstr)) { if (out_attribute(soap, ns->id, att->name, att->data)) return soap->error; break; } } if (!ns || !ns->id) { sprintf(soap->msgbuf, "xmlns:"SOAP_DOMID_FORMAT, soap->idnum++); if (soap_attribute(soap, soap->msgbuf, att->nstr)) return soap->error; strcat(soap->msgbuf, ":"); strcat(soap->msgbuf, att->name); if (soap_attribute(soap, soap->msgbuf + 6, att->data)) return soap->error; } } } else if (soap_attribute(soap, att->name, att->data)) return soap->error; } } if (soap_element_start_end_out(soap, NULL)) return soap->error; if (node->data || node->wide || node->elts) { if (node->data) { if (soap_string_out(soap, node->data, 0)) return soap->error; } else if (node->wide) { if (soap_wstring_out(soap, node->wide, 0)) return soap->error; } else { for (elt = node->elts; elt; elt = elt->next) if (soap_out_xsd__anyType(soap, tag, 0, elt, NULL)) return soap->error; } } DBGLOG(TEST, SOAP_MESSAGE(fdebug, "End of DOM node '%s'\n", tag + colon)); if (soap_send_raw(soap, "</", 2)) return soap->error; if (prefix) if (soap_send(soap, prefix) || soap_send_raw(soap, ":", 1)) return soap->error; if (soap_send(soap, tag + colon) || soap_send_raw(soap, ">", 1)) return soap->error; if (p) p->level = 0; /* xmlns binding is out of scope */ } return SOAP_OK; }
int html_form_options(struct soap *soap, struct option *options) { soap_send(soap, "<form action='' method='get' name='form'>"); html_options(soap, options); return soap_send(soap, "</form>"); }
int html_options(struct soap *soap, struct option *options) { struct option *p; soap_send(soap, "<table border='0' cellspacing='0' cellpadding='0' bgcolor='#666666' nosave>\n"); soap_send(soap, "<tr height='10'><td background='btl.gif'></td><td background='bt.gif'></td><td background='bt.gif'></td><td background='bt.gif'></td><td background='bt.gif'></td><td background='bt.gif'></td><td width='10' background='btr.gif'></td><td width='10' height='10' background='obls.gif'></td></tr>"); for (p = options; p->name; p++) { const char *s, *t, *n; int i; n = p->name; if (n[0] && n[1] == '.') n += 2; s = p->selections; soap_send(soap, "<tr><td background='bl.gif'></td><td align='right'>"); if (!n[0]) { if (s) soap_send(soap, s); soap_send(soap, "</td><td></td><td></td><td></td><td>"); if (p->value) soap_send(soap, p->value); soap_send(soap, "</td>"); } else if (!s) { soap_send(soap, n); soap_send(soap, "</td><td width='10' background='ls.gif'></td><td bgcolor='#FFFFFF'><a href='?"); soap_send(soap, n); if (p->selected) soap_send(soap, "='><img src='checked.gif' align='absmiddle' border='0'></a></td><td width='10' background='rs.gif'></td><td>on</td>"); else soap_send(soap, "'><img src='unchecked.gif' align='absmiddle' border='0'></a></td><td width='10' background='rs.gif'></td><td>off</td>"); } else if (strchr(s, ' ')) { soap_send(soap, n); soap_send(soap, "</td>"); for (i = 0; ; i++) { t = strchr(s, ' '); if (i == 0) soap_send(soap, "<td width='10' background='ls.gif'></td><td bgcolor='#FFFFFF'>"); else soap_send(soap, "<tr><td background='bl.gif'></td><td></td><td width='10' background='ls.gif'></td><td bgcolor='#FFFFFF'>"); if (i == p->selected) soap_send(soap, "<img src='selected.gif' align='absmiddle' border='0'></td><td width='10' background='rs.gif'></td><td>"); else { soap_send(soap, "<a href='?"); soap_send(soap, n); soap_send(soap, "="); if (t) soap_send_raw(soap, s, t - s); else soap_send(soap, s); soap_send(soap, "'>"); soap_send(soap, "<img src='deselected.gif' align='absmiddle' border='0'></a></td><td width='10' background='rs.gif'></td><td>"); } if (t) soap_send_raw(soap, s, t - s); else soap_send(soap, s); soap_send(soap, "</td>"); if (!t) break; soap_send(soap, "<td width='10' background='br.gif'></td><td width='10' background='ls.gif'></td></tr>\n"); s = t + 1; } } else { char buf[16]; soap_send(soap, n); soap_send(soap, "</td><td width='10' background='ls.gif'></td><td bgcolor='#FFFFFF'> </td><td width='10' background='rs.gif'></td><td><input type='text' name='"); soap_send(soap, n); soap_send(soap, "' value='"); if (p->value) soap_send(soap, p->value); soap_send(soap, "' size='"); sprintf(buf, "%d", p->selected > 0 ? p->selected : 24); soap_send(soap, buf); soap_send(soap, "'><input type='submit' value='Set "); soap_send(soap, s); soap_send(soap, "'></td>"); } soap_send(soap, "<td width='10' background='br.gif'></td><td width='10' background='ls.gif'></td></tr>\n"); // <tr height='1'><td height='1' background='bl.gif'></td><td></td><td></td><td></td><td></td><td width='10' height='1' background='br.gif'></td><td width='10' height='1' background='ls.gif'></td></tr>\n"); } soap_send(soap, "<tr height='10'><td background='bbl.gif'></td><td background='bb.gif'></td><td background='bb.gif'></td><td background='bb.gif'></td><td background='bb.gif'></td><td background='bb.gif'></td><td width='10' background='bbr.gif'><td width='10' height='10' background='ls.gif'</td></tr>\n<tr height='10'><td width='10' height='10' background=otrs.gif></td><td height='10' background='ts.gif'></td><td height='10' background='ts.gif'></td><td height='10' background='ts.gif'></td><td height='10' background='ts.gif'></td><td height='10' background='ts.gif'></td><td height='10' background='ts.gif'></td><td width='10' height='10' background='otls.gif'></td></tr>\n</table>\n"); return SOAP_OK; }