// ============================================================================ // C servlet sample for the G-WAN Web Application Server (http://trustleap.ch/) // ---------------------------------------------------------------------------- // tidy.c: use the 'libtidy' C library to help clean-up/validate HTML pages // just like the mod_tidy Apache module. // // Installation of the libtidy library (Debian): // sudo apt-get install -y libtidy-dev libtidy-0.99-0 // ---------------------------------------------------------------------------- // Usage: http://127.0.0.1:8080/?tidy&file=index.html // // Output: // Tidying '/gwan/0.0.0.0_8080/#0.0.0.0/www/index.html' // Diagnostics: // line 42 column 3 - Warning: missing before
usage: http://127.0.0.1:8080/?tidy&file=index.html
"); return HTTP_200_OK; } // ------------------------------------------------------------------------- // build the local file name // ------------------------------------------------------------------------- char *wwwpath = (char*)get_env(argv, WWW_ROOT); char filename[512] = {0}; snprintf(filename, sizeof(filename), "%s/%s", wwwpath, file); u32 i = 1; for(; i < argc; i++) // just in case we have subdirectories { strcat(filename, "/"); strcat(filename, argv[i]); } // ------------------------------------------------------------------------- // try to load the file // ------------------------------------------------------------------------- //printf("loading '%s' file...\n", filename); xbuf_t f; xbuf_init(&f); xbuf_frfile(&f, filename); xbuf_xcat (reply, "%s file %s
", f.len ? "Tidying" : "Can't find", filename); //printf("loaded %d bytes\n", f.len); if(!f.len) return HTTP_200_OK; // ------------------------------------------------------------------------- // now we have loaded the file, process it with libtidy // ------------------------------------------------------------------------- //puts("running libtidy..."); TidyBuffer output = {0}; TidyBuffer errbuf = {0}; TidyDoc tdoc = tidyCreate(); // initialize "document" int ret = tidyOptSetBool(tdoc, TidyXhtmlOut, no); // convert to XHTML if(ret != 0) ret = tidySetErrorBuffer(tdoc, &errbuf); // capture diagnostics if(ret >= 0) ret = tidyParseString(tdoc, f.ptr); // parse the file if(ret >= 0) ret = tidyCleanAndRepair(tdoc); // tidy it up! if(ret >= 0) ret = tidyRunDiagnostics(tdoc); if(ret > 1) ret = tidyOptSetBool(tdoc, TidyForceOutput, yes) ? ret : -1; if(ret >= 0) ret = tidySaveBuffer(tdoc, &output); if(ret >= 0) { if(ret > 0) xbuf_xcat(reply, "Diagnostics:
\n%H
A severe error (%d) occurred.
Click here" " to see the 'enhanced' %s file.
", file, file); } // ------------------------------------------------------------------------- // free Tidy context // ------------------------------------------------------------------------- tidyBufFree(&output); tidyBufFree(&errbuf); tidyRelease(tdoc); return HTTP_200_OK; // return an HTTP code } // ============================================================================ // End of Source Code // ============================================================================