libtabula

Check-in [4234731834]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:examples/cgi_jpeg now modifies its output to use LF only in output when running under dtest so the bmark.txt output file doesn't have mixed line endings. There's no reason we have to speak legal HTTP in this case.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 42347318341c46defc5ae32159a0ba4c4456debb
User & Date: tangent 2015-08-14 03:54:38
Context
2015-08-14
03:55
Dropped the versioned crnl-glob Fossil setting. All *.md and *.txt are now LF-only. check-in: c37cba89a5 user: tangent tags: trunk
03:54
examples/cgi_jpeg now modifies its output to use LF only in output when running under dtest so the bmark.txt output file doesn't have mixed line endings. There's no reason we have to speak legal HTTP in this case. check-in: 4234731834 user: tangent tags: trunk
2015-08-12
21:53
Removed "Initializing result set objects" from the "tricky bits" section of the Wishlist section on DB independence, since it now basically boils down to the Row and Field items just added, and those are more specific. check-in: 97073e9aa4 user: etr tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to examples/cgi_jpeg.cpp.

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
..
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
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 USA
***********************************************************************/

#include "cmdline.h"
#include "images.h"

#define CRLF			"\r\n"
#define CRLF2			"\r\n\r\n"

int
main(int argc, char* argv[])
{
	// Get database access parameters from command line if present, else
	// use hard-coded values for true CGI case.
	libtabula::examples::CommandLine cmdline(argc, argv, "root",
			"nunyabinness");
	if (!cmdline) {
		return 1;
	}






	// Parse CGI query string environment variable to get image ID
	unsigned int img_id = 0;
	char* cgi_query = getenv("QUERY_STRING");
	if (cgi_query) {
		if ((strlen(cgi_query) < 4) || memcmp(cgi_query, "id=", 3)) {
			std::cout << "Content-type: text/plain" << std::endl << std::endl;
................................................................................
				cmdline.server(), cmdline.user(), cmdline.pass());
		libtabula::Query query = con.query();
		query << "SELECT * FROM images WHERE id = " << img_id;
		libtabula::StoreQueryResult res = query.store();
		if (res && res.num_rows()) {
			images img = res[0];
			if (img.data.is_null) {
				std::cout << "Content-type: text/plain" << CRLF2;
				std::cout << "No image content!" << CRLF;
			}
			else {
				std::cout << "X-Image-Id: " << img_id << CRLF; // for debugging
				std::cout << "Content-type: image/jpeg" << CRLF;
				std::cout << "Content-length: " <<
						img.data.data.length() << CRLF2;
				std::cout << img.data;
			}
		}
		else {
			std::cout << "Content-type: text/plain" << CRLF2;
			std::cout << "ERROR: No image with ID " << img_id << CRLF;
		}
	}
	catch (const libtabula::BadQuery& er) {
		// Handle any query errors
		std::cout << "Content-type: text/plain" << CRLF2;
		std::cout << "QUERY ERROR: " << er.what() << CRLF;
		return 1;
	}
	catch (const libtabula::Exception& er) {
		// Catch-all for any other libtabula exceptions
		std::cout << "Content-type: text/plain" << CRLF2;
		std::cout << "GENERAL ERROR: " << er.what() << CRLF;
		return 1;
	}

	return 0;
}







<
<
<










>
>
>
>
>







 







|
|


|
|

|




|
|




|
|




|
|





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
..
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
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
 USA
***********************************************************************/

#include "cmdline.h"
#include "images.h"




int
main(int argc, char* argv[])
{
	// Get database access parameters from command line if present, else
	// use hard-coded values for true CGI case.
	libtabula::examples::CommandLine cmdline(argc, argv, "root",
			"nunyabinness");
	if (!cmdline) {
		return 1;
	}

	// Define line ending style used in output, varying depending on
	// whether the output is CGI/HTTP or the bmark.txt file for dtest.
	const char* eol1 = cmdline.dtest_mode() ? "\n" : "\r\n";
	const char* eol2 = cmdline.dtest_mode() ? "\n" : "\r\n\r\n";

	// Parse CGI query string environment variable to get image ID
	unsigned int img_id = 0;
	char* cgi_query = getenv("QUERY_STRING");
	if (cgi_query) {
		if ((strlen(cgi_query) < 4) || memcmp(cgi_query, "id=", 3)) {
			std::cout << "Content-type: text/plain" << std::endl << std::endl;
................................................................................
				cmdline.server(), cmdline.user(), cmdline.pass());
		libtabula::Query query = con.query();
		query << "SELECT * FROM images WHERE id = " << img_id;
		libtabula::StoreQueryResult res = query.store();
		if (res && res.num_rows()) {
			images img = res[0];
			if (img.data.is_null) {
				std::cout << "Content-type: text/plain" << eol2;
				std::cout << "No image content!" << eol1;
			}
			else {
				std::cout << "X-Image-Id: " << img_id << eol1; // for debugging
				std::cout << "Content-type: image/jpeg" << eol1;
				std::cout << "Content-length: " <<
						img.data.data.length() << eol2;
				std::cout << img.data;
			}
		}
		else {
			std::cout << "Content-type: text/plain" << eol2;
			std::cout << "ERROR: No image with ID " << img_id << eol1;
		}
	}
	catch (const libtabula::BadQuery& er) {
		// Handle any query errors
		std::cout << "Content-type: text/plain" << eol2;
		std::cout << "QUERY ERROR: " << er.what() << eol1;
		return 1;
	}
	catch (const libtabula::Exception& er) {
		// Catch-all for any other libtabula exceptions
		std::cout << "Content-type: text/plain" << eol2;
		std::cout << "GENERAL ERROR: " << er.what() << eol1;
		return 1;
	}

	return 0;
}