php zlib compression support won't work - zlib

If I'm setting up in my php.ini the zlib compression support, won't really seems to work.
zlib.output_compression = On
; http://php.net/zlib.output-compression-level
zlib.output_compression_level = -1
; You cannot specify additional output handlers if zlib.output_compression
; is activated here. This setting does the same as output_handler but in
; a different order.
; http://php.net/zlib.output-handler
;zlib.output_handler =
I'm using php-5.3.9
I have been testing with the following php code, I must say I'm not a professional but seems like don't work.
<?php
$filename = tempnam('/tmp', 'zlibtest') . '.gz';
echo "<html>\n<head></head>\n<body>\n<pre>\n";
$s = "Only a test, test, test, test, test, test, test, test!\n";
// open file for writing with maximum compression
$zp = gzopen($filename, "w9");
// write string to file
gzwrite($zp, $s);
// close file
gzclose($zp);
// open file for reading
$zp = gzopen($filename, "r");
// read 3 char
echo gzread($zp, 3);
// output until end of the file and close it.
gzpassthru($zp);
gzclose($zp);
echo "\n";
// open file and print content (the 2nd time).
if (readgzfile($filename) != strlen($s)) {
echo "Error with zlib functions!";
}
unlink($filename);
echo "</pre>\n</body>\n</html>\n";
?>
there is no output

The zlib output handler has nothing to do with th gz file functions.
So there is something else wrong, turn on error_reporting.
Otherwise the shown code is working fine. The "no output" behaviour is not reproducible.

Related

Best way to append text to file in dm-script

What is the best way to append line(s) to a file?
Currently I am using the following script:
/**
* Append the `line` to the file given at the `path`.
*
* #param path
* The absolute or relative path to the file with
* extension
* #param line
* The line to append
* #param [max_lines=10000]
* The maximum number of lines to allow for a file
* to prevent an infinite loop
*/
void append(string path, string line, number max_lines){
number f = OpenFileForReadingAndWriting(path);
// go through file until the end is reached to set the
// internal pointer to this position
number line_counter = 0;
string file_content = "";
string file_line;
while(ReadFileLine(f, file_line) && line_counter < max_lines){
line_counter++;
// file_content += file_line;
}
// result("file content: \n" + file_content + "{EOF}");
// append the line
WriteFile(f, line + "\n");
CloseFile(f);
}
void append(string path, string line){
append(path, line, 10000);
}
string path = "path/to/file.txt";
append(path, "Appended line");
For me it seems a little bit odd to read the whole file content to just append one line. If the file is very big, this probably is very slow1. So I guess there is a better solution of this. Does anyone know this solution?
Some background
My application is written in python but executed in Digital Micrograph. My python application is logging its steps. Sometimes I am executing dm-script from python. There I have no possibility to see what is going on. Since there is a bug, I need something to find out what is going on. Therefore I want to add logging to dm-script too.
This also explains, why I want to open and close the file every single time. This takes more time, but I don't care about execution speed while debugging. The logs will either be removed or switched off for the normal version, as usual. But on the other hand I am executing dm-script and python alternating so I have to prevent python blocking the file for dm-script and the other way around.
1As written in the background, I am not really interested in speed. So the current script is enough for me. Still I am interested in how to do this better, just for learnings and curiositys sake.
The best way to deal with any files in DM-script (binary or text) is to use the streaming object. The following example should answer your question:
void writeText()
{
string path
if ( !SaveAsDialog( "Save text as" , path , path ) ) return
number fileID = CreateFileForWriting( path )
object fStream = NewStreamFromFileReference( fileID , 1 ) // 1 for auto-close file when out of scope
// Write some text
number encoding = 0 // 0 = system default
fStream.StreamWriteAsText( encoding , "The quick brown dog jumps over the lazy fox" )
// Replace last 'fox' by 'dog'
fStream.StreamSetPos( 1 , -3 ) // 3 bytes before current position
fStream.StreamWriteAsText( encoding, "dog" )
// Replace first 'dog' by 'fox'
fStream.StreamSetPos( 0 , 16 ) // 16 bytes after start
fStream.StreamWriteAsText( encoding, "fox" )
// Append at end
fStream.StreamSetPos( 2 , 0 ) // end position (0 bytes from end)
fStream.StreamWriteAsText( encoding, "." )
}
writeText()

SSL connection fails when stalled every ~5s

I have a Perl streaming application that GET some file through HTTPS (for RadioParadise). The TCP flow is often paused for a few seconds because the player buffers a lot. As a result, the connection very often breaks/ends early.
I've taken various TCP logs and wireshark seems to be all confused and whines about Changing Cipher Spec and Ignored Unknown records. Obviously, the Change Cipher packets are wrong and do not match re-handshaking, there is something funny in the packets. What the logs surely shows is in the case of early termination, the server is closing the TCP connection (proper FIN frame)
I've written a small Perl example that throttles the download by using varous length pauses to show what happens. If the download is not paused by more than 1 sec, then this does not happen. When it is paused by 5s, it always happens at some point of the download. Server's openSSL is 1.0.2k and I've tried client 1.1.0j and various version from 0.9.8 to 1.0.2. I've tried also various Perl versions, no change
(it's just an example that actually shows the problem, not supposed to be proper code)
I've also written the same application in C to eliminate Perl as a potential problem and got exactly the same result.
I've also tried other HTTPS server and they do not have the same problem, it seems to be related to RP's. Maybe somebody would have an idea of what I should look at for further investigation as currently, I don't know in what direction to continue. Some general advice on why pausing for 5 sec would make any difference at TCP or SSL level (it's not a SO_KEEPALIVE issue).
NB: there is no packet lost by kernel in my tcpdump
use IO::Socket::SSL;
use Net::SSLeay;
use IO::Select;
$IO::Socket::SSL::DEBUG = 3;
$Net::SSLeay::trace = 3;
my $sock = IO::Socket::SSL->new(
Timeout => 15,
PeerAddr => '23.29.117.2',
PeerPort => 443,
SSL_startHandshake => 1,
SSL_verify_mode => Net::SSLeay::VERIFY_NONE() # SSL_VERIFY_NONE isn't recognized on some platforms?!?, and 0x00 isn't always "right"
) or do {
print("Couldn't create socket binding to $!");
return undef;
};
my $sel = IO::Select->new($sock);
my $start = time;
use constant SLEEP => 5;
syswrite ($sock, "GET /blocks/chan/0/4/1835801-1835807.flac HTTP/1.0\r\n\r\n");
sysread( $sock, my $data, 512 );
print $data;
$sock->blocking(0);
my $total;
my $sum;
while (1) {
my $bytes = sysread( $sock, my $data, 8192 );
next if !defined $bytes;
last if !$bytes;
$total += $bytes;
$sum += $bytes;
print "$total ", int ($total/(time() - $start)/1000), " kB/s \r" if time() - $start;
if ($sum > SLEEP*1024*1024/8) {
sleep(SLEEP);
$sum = 0;
}
}
print("\nfinished $total\n");
$sock->close();

Replacing string within a file using bash + environmental variables

Let's say for simplicities sake I have a file (please forgive my useless pseudo code)
file.txt
std::string filename = "filename.txt"
double v_no = 2.0;
const int v_minor = 0; // < --- Target
std::string random_var1 = "Hello"
std::string random_var2 = "Hello 2"
int main()
{
// ..
}
And I have a bash file in the same directory - set_version.sh
I want to replace a string in this file with this script - specifically "v_minor = 0" with "v_minor = $VARIABLE" - in my case the variable will be an environmental set on a server.
So lets say it has been successfully run a couple of times. Now the string reads "v_minor = 2". I still want the same set_version.sh script to change 2 to whatever the variable is.
In the windows build of my software I have a batch file that changes "v_minor = %d" to "v_minor = %VERSION%".
My question is how I do something similar in bash? E.g. ignoring what the decimal is in the string and changing it to variable.
What I've got so far:
set_version.sh
#!/bin/bash
VERSION=75
sed -i '' 's/v_minor = %d/v_minor = $VERSION/g/' file.txt
Version var being set is just for testing purposes.
This returns error
sed: 1: "s/v_minor = %d/v_minor ...": bad flag in substitute command: '/'
I'm running Mac OS X Yosemite for this test.
Again, essentially %d can be any integer.
Thank you
That will work for you:
sed -i '' "s/v_minor = .*$/v_minor = $VERSION/g" file.txt
.*$ means till the end of that string.
Don't forget to use " " when operating with variables.
sed -i '' 's/v_minor = %d/v_minor = $VERSION/g/' file.txt
# ^
# /
# remove this slash ---
According to your description, I would suggest another easier way as follows (and simplicity will make less bugs...):
First, Change your target to
const int v_minor = V_MINOR; // < --- Target
Second, add an include line, anywhere before the target statement:
#include "version.h"
Third, write a script to generate the version.h similar to the followings:
#ifndef _VERSION_H_
#define V_MINOR 0 // <== this 0 is what you want to change.
#endif
Generate a script to output the said version.h is too simple (Just some fixed prints plus the target number). Thus, I don't provide it here.
Comparing to those possible error-prone sed-awk-perl solution, I prefer this simple solution.

Calling new PostgreSQL command from Java Application issue

I have defined a new command my_command in PostgreSQL. This command takes the path of ANALYZE and inside analyze.c, I have a function to do some operations if its my_command.This command takes the input arguments: table name, column name and an input string.
When I run this command from command line psql, it works as expected. But when I call the same command from a java application, all the variables that store the input arguments are NULL.
I printed the value of the input string in gram.y file where I have defined my_command.
fprintf (stderr, "I am inside gram.y %s\n",n->inp_str);
and the input string is printed correctly.
But when I print stmt->inp_str in the function standard_ProcessUtility() of utility.c for the case T_VacuumStmt, I get the value as NULL. This is as far as I could trace back from analyze.c.
I am not sure how running the command from an application can make a difference.
gram.y content gist:
MyStmt:
my_keyword qualified_name name_list my_inp_str
{
VacuumStmt *n = makeNode(VacuumStmt);
n->options = VACOPT_ANALYZE;
n->freeze_min_age = -1;
n->freeze_table_age = -1;
n->relation = $2;
n->va_cols = $3;
n->inp_str = $4;
fprintf (stderr, "I am inside gram.y %s\n",n->inp_str);
$$ = (Node *)n;
}
;
char *inp_str is added to the struct VacuumStmt in parsenodes.h
Any help is appreciated. Thanks!
I had mailed this question to the pgsql-hackers mailing list and I got a response that I had to add code to copy inp_str to _copyVacuumStmt().
Once this was done, the command worked from the Java application too. I have asked for the reason why it worked from command line even without adding the above code. I will update the details once I get the details.

Arduino Serial.println() outputs a blank line if not in loop()

I'm attempting to write a function that will pull text from different sources (Ethernet client/Serial/etc.) into a single line, then compare them and run other functions based on them. Simple..
And while this works, I am having issues when trying to call a simple Serial.println() from a function OTHER than loop().
So far, I have around 140 lines of code, but here's a trimmed down version of the portion that's causing me problems:
boolean fileTerm;
setup() {
fileTerm = false;
}
loop() {
char character;
String content="";
while (Serial.available()) {
character = Serial.read();
content.concat(character);
delay(1);
}
if (content != "") {
Serial.println("> " + content);
/** Error from Serial command string.
* 0 = No error
* 1 = Invalid command
*/
int err = testInput(content);
}
int testInput(String content) {
if (content == "term") {
fileTerm = true;
Serial.println("Starting Terminal Mode");
return 0;
}
if (content == "exit" && fileTerm == true) {
fileTerm = false;
Serial.println("Exiting Terminal Mode");
return 0;
}
return 1;
}
(full source at http://pastebin.com/prEuBaRJ)
So the point is to catch the "term" command and enter some sort of filesystem terminal mode (eventually to access and manipulate files on the SD card). The "exit" command will leave the terminal mode.
However, whenever I actually compile and type these commands with others into the Serial monitor, I see:
> hello
> term
> test for index.html
> exit
> test
> foo
> etc...
I figure the function is catching those reserved terms and actually processing them properly, but for whatever reason, is not sending the desired responses over the Serial bus.
Just for the sake of proper syntax, I am also declaring the testInput() function in a separate header, though I would doubt this has any bearing on whether or not this particular error would occur.
Any explainable reason for this?
Thanks.
Model: Arduino Uno R3, IDE version: 1.0.4, though this behavior also happened on v1.0.5 in some instances..
It is kinda guessable how you ended up putting delay(1) in your code, that was a workaround for a bug in your code. But you didn't solve it properly. What you probably saw was that your code was too eager to process the command, before you were done typing it. So you slowed it down.
But that wasn't the right fix, what you really want to do is wait for the entire command to be typed. Until you press the Enter key on your keyboard.
Which is the bug in your code right now, the content variable doesn't just contain "term", it also contains the character that was generated by your terminal's Enter key. Which is why you don't get a match.
So fix your code, add a test to check that you got the Enter key character. And then process the command.

Resources