LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-12-2005, 11:34 PM   #1
Roman_Rus
LQ Newbie
 
Registered: Jul 2005
Posts: 4

Rep: Reputation: 0
cgi the program on C ++, compressed HTTP


Hi all!

Who be created function for a compression of a content in cgi to the program written on C?

Share a simple example!

Has written the for gzip and deflate but does not work: (

Code:
#include <stdio.h>
#include <assert.h>

/**/ 
#include <zlib.h>

static void *g_zalloc_wrapper(voidpf opaque, uInt items, uInt size) {
	void *ptr = malloc(items * size);
	return ptr;
}

static void g_zfree_wrapper(voidpf opaque, voidpf address) {
	free(address);
}


/// gzip = 1 > gzip, gzip = 0 > deflate
static int zlib_encode(Bytef *mg, int gzip)
{
static int gz_magic[2] = {0x1f, 0x8b}; 
#define OS_CODE			0x03  /*FIXME */

	z_stream stream;
    //char *new_body;

	stream.next_in = mg;
	stream.avail_in = strlen(mg);
	
	stream.total_in = 0;
	
	//new_body = 
		stream.next_out = malloc(strlen(mg) + 75); /* overly generous */

    assert(stream.next_out);

	

	stream.avail_out = strlen(mg) + 64;
	stream.total_out = 0;
	
	stream.zalloc = g_zalloc_wrapper;
	stream.zfree = g_zfree_wrapper;
	stream.opaque = NULL;
	
	stream.data_type = Z_ASCII;

/* 
*/
	if (deflateInit2(
		    &stream, 
		    3 /* compression level, 1-9, default 6 (Z_DEFAULT_COMPRESSION) */, 
		    Z_DEFLATED, 
		    ((gzip!=0)?16:0)+15 /* gzip headers? (16) + window size (15b) */, 
		    3 /* memory level, 1-9, default 8 */, 
		    Z_DEFAULT_STRATEGY
		    ) != Z_OK) {
		/* failed to initialize, bail out */
	 	deflateEnd(&stream);
		return -1;
	}
		 
	while (stream.avail_in != 0) {
		switch(deflate(&stream, Z_FINISH)) {
		case Z_STREAM_END: /* success */
			break;
			
		case Z_OK: /* need more buffer space !?  bail out... */
			deflateEnd(&stream);
			return -1;
			
		default: /* other error cases */
			deflateEnd(&stream); 
			return -1;
		}
	}
	
if(gzip!=0) 
{
 	printf("Content-Encoding: gzip\n\r");	 
}
else
{
	printf("Content-Encoding: deflate\n\r"); 
}

printf("Content-type: text/html\n\r\n\r"); 

int ifd = dup(fileno(stdout)); 
FILE *Out = fdopen(ifd, "w+b");


if(gzip!=0) {
	char *s2[10];
	        s2[0] = gz_magic[0];
	        s2[1] = gz_magic[1];
	        s2[2] = Z_DEFLATED;
	        s2[3] = s2[4] = s2[5] = s2[6] = s2[7] = s2[8] = 0;  
	        s2[9] = OS_CODE;

			fwrite(&s2 , 10 , 1 ,Out);
}

fwrite(&stream.next_out , stream.total_out , 1 ,Out);

 

if(gzip!=0) {
	char *trailer[9];

			uLong crc = crc32(0L, Z_NULL, 0);

			crc = crc32(crc, (const Bytef *) mg, strlen(mg));

			/* write crc & stream.total_in in LSB order */
			trailer[0] = (char) crc & 0xFF;
			trailer[1] = (char) (crc >> 8) & 0xFF;
			trailer[2] = (char) (crc >> 16) & 0xFF;
			trailer[3] = (char) (crc >> 24) & 0xFF;
			trailer[4] = (char) stream.total_in & 0xFF;
			trailer[5] = (char) (stream.total_in >> 8) & 0xFF;
			trailer[6] = (char) (stream.total_in >> 16) & 0xFF;
			trailer[7] = (char) (stream.total_in >> 24) & 0xFF;
			trailer[8] = '\0';

			fwrite(&trailer , 9 , 1 ,Out);
}
deflateEnd(&stream);
}


 



int main()
{

	
zlib_encode ("<html>test gggkljgjklhkj hkjhlkjhkljhl jkhkj hlkjhlkjhlkh lkjhlkjhlkh lkhlkjjh lhj lkk hjk gdfggdfgdg"
			 "ghfjghjgj ghhhhhjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj gfjfjghfgjg                     gghjgjghjg"
			 "gfjgfhjfjg ghjggggggggggggjjgjghjgjg gjgjfhjfghfjgjgf gjfjghgf fgjhgj fgj gjghj jgjhgf fgj fgjg"
			 "yujjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjgjhgjghjgjhghgjghjgjghjghh"
			 "fghfhfhhldsgffdhgjkdhgdklghkjhdlkfjhgldhgklshdljkfghkjsdhgldshglkjdhglkdshglkdhglkdhgldhg</html>", 0);
 

}
 
Old 07-15-2005, 09:46 PM   #2
Roman_Rus
LQ Newbie
 
Registered: Jul 2005
Posts: 4

Original Poster
Rep: Reputation: 0
There are no opinions?
 
Old 07-17-2005, 06:09 PM   #3
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Your question is a little confusing... maybe you can reword it? I tried reading your code but I'm not that good yet...
 
Old 07-21-2005, 06:10 PM   #4
Roman_Rus
LQ Newbie
 
Registered: Jul 2005
Posts: 4

Original Poster
Rep: Reputation: 0
I need to make in the cgi program function such as, ob_start ("ob_gzhandler"); in PHP which compresses a content.

That I have found the only thing is above example, but it does not work. How correctly to make it?
 
Old 07-21-2005, 11:31 PM   #5
sind
Member
 
Registered: Jun 2005
Posts: 75

Rep: Reputation: 15
In what way doesn't it work?

Compile error?
Crash?
Web server error?
Unexpected result?

What is the setup you are using to test it? For instance, which web server and which web browser?

~sind
 
Old 07-22-2005, 03:41 PM   #6
Roman_Rus
LQ Newbie
 
Registered: Jul 2005
Posts: 4

Original Poster
Rep: Reputation: 0
Web browser shows empty a web page.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP! CGI problem : HTTP 500 - Internal server error eech55 Programming 2 10-11-2004 11:27 AM
How to uninstall program compressed with tar.gz ronronson Linux - Newbie 1 06-11-2004 03:50 AM
C++ CGI Program(Tell Me what you think) karlan Programming 0 03-29-2004 01:33 PM
Smoothwall HTTP and CGI update problem phantompgr Linux - Networking 3 09-24-2002 03:56 PM
http://www.burstnet.com/cgi-bin/ads/ad7954a.cgi/3980/RETURN-CODE rverlander LQ Suggestions & Feedback 1 06-07-2002 07:35 AM


All times are GMT -5. The time now is 03:18 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration