Begin separating YADIS handling from XRDS parsing
[citadel.git] / citadel / modules / openid / serv_openid_rp.c
index 1e50af10bcf8fa2e756c50314ad703df75638f75..aa1ba633e41d19b3bb04bd1d1319e73b7277f8b6 100644 (file)
@@ -42,6 +42,7 @@
 #include <string.h>
 #include <limits.h>
 #include <curl/curl.h>
+#include <expat.h>
 #include "ctdl_module.h"
 #include "config.h"
 #include "citserver.h"
@@ -598,6 +599,71 @@ int fetch_http(StrBuf *url, StrBuf **target_buf)
 }
 
 
+
+struct xrds {
+       int nesting_level;
+       int in_xrd;
+};
+
+
+void xrds_xml_start(void *data, const char *supplied_el, const char **attr) {
+       struct xrds *xrds = (struct xrds *) data;
+
+       ++xrds->nesting_level;
+
+       if (!strcasecmp(supplied_el, "XRD")) {
+               ++xrds->in_xrd;
+               syslog(LOG_DEBUG, "*** XRD DOCUMENT BEGIN ***");
+       }
+}
+
+
+void xrds_xml_end(void *data, const char *supplied_el) {
+       struct xrds *xrds = (struct xrds *) data;
+
+       --xrds->nesting_level;
+
+       if (!strcasecmp(supplied_el, "XRD")) {
+               --xrds->in_xrd;
+               syslog(LOG_DEBUG, "*** XRD DOCUMENT END ***");
+       }
+}
+
+
+void xrds_xml_chardata(void *data, const XML_Char *s, int len) {
+       struct xrds *xrds = (struct xrds *) data;
+       
+       syslog(LOG_DEBUG, "%2d xrds_xml_chardata()", xrds->nesting_level);
+       /* StrBufAppendBufPlain (xrds->CData, s, len, 0); */
+}
+
+
+/*
+ * Parse an XRDS document.
+ * If OpenID stuff is discovered, populate FIXME something and return nonzero
+ * If nothing useful happened, return 0.
+ */
+int parse_xrds_document(StrBuf *ReplyBuf) {
+       struct xrds xrds;
+
+       memset(&xrds, 0, sizeof (struct xrds));
+       XML_Parser xp = XML_ParserCreate(NULL);
+       if (xp) {
+               XML_SetUserData(xp, &xrds);
+               XML_SetElementHandler(xp, xrds_xml_start, xrds_xml_end);
+               XML_SetCharacterDataHandler(xp, xrds_xml_chardata);
+               XML_Parse(xp, ChrPtr(ReplyBuf), StrLength(ReplyBuf), 0);
+               XML_Parse(xp, "", 0, 1);
+               XML_ParserFree(xp);
+       }
+       else {
+               syslog(LOG_ALERT, "Cannot create XML parser");
+       }
+
+       return(0);
+}
+
+
 /*
  * Attempt to perform YADIS discovery.
  * If successful, returns nonzero and fills the session's claimed ID blah FIXME this comment
@@ -606,6 +672,8 @@ int fetch_http(StrBuf *url, StrBuf **target_buf)
 int perform_yadis_discovery(StrBuf *YadisURL) {
        int docbytes = (-1);
        StrBuf *ReplyBuf = NULL;
+       int r;
+
 
        docbytes = fetch_http(YadisURL, &ReplyBuf);
        if (docbytes < 0) {
@@ -616,12 +684,9 @@ int perform_yadis_discovery(StrBuf *YadisURL) {
                return(0);
        }
 
-       /* ok we have something here.  is it an XRDS document? */
-
-       /* FIXME finish this */
-
+       r = parse_xrds_document(ReplyBuf);
        FreeStrBuf(&ReplyBuf);
-       return(0);
+       return(r);
 }
 
 
@@ -665,9 +730,14 @@ void cmd_oids(char *argbuf) {
 
        /********** OpenID 2.0 section 7.3 - Discovery **********/
 
-       /* First we're supposed to attempt XRI.  What the fuck is XRI and why do I care about it? */
+       /* First we're supposed to attempt XRI based resolution.
+        * No one is using this, no one is asking for it, no one wants it.
+        * So we're not even going to bother attempting this mode.
+        */
 
-       /* Second we attempt YADIS.  Google uses this so we'd better do our best to implement it. */
+       /* Second we attempt YADIS.
+        * Google uses this so we'd better do our best to implement it.
+        */
        int yadis_succeeded = perform_yadis_discovery(oiddata->claimed_id);
 
        /* Third we attempt HTML-based discovery.  Here we go! */