2012-04-03 15 views
7

Dopo aver effettuato una richiesta di gzip sgonfiare in PHP, ricevo la stringa sgonfio in blocchi sfalsati, che si presenta come il seguenteCome decodificare/gonfiare una stringa gzip Chunked?

Esempio accorciato notevolmente mostrare Formato:

00001B4E 
¾”kŒj…Øæ’ìÑ«F1ìÊ`+ƒQì¹UÜjùJƒZ\µy¡ÓUžGr‡J&=KLËÙÍ~=ÍkR 
0000102F 
ñÞœÞôΑüo[¾”+’Ñ8#à»0±R-4VÕ’n›êˆÍ.MCŽ…ÏÖr¿3M—èßñ°r¡\+ 
00000000 

non riesco a gonfiare che presumibilmente a causa del formato chunked. Posso confermare che i dati non sono corretti dopo aver rimosso manualmente gli offset con un editor esadecimale e aver letto l'archivio gzip. Mi chiedo se c'è un metodo corretto per analizzare questa risposta sgonfiata gzip in una stringa leggibile?

Potrei essere in grado di suddividere questi offset e unire i dati insieme in una stringa per chiamare gzinflate, ma sembra che ci debba essere un modo più semplice.

risposta

9

Il metodo corretto per sgonfiare un Chunked la risposta è più o meno come segue:

initialise string to hold result 
for each chunk { 
    check that the stated chunk length equals the string length of the chunk 
    append the chunk data to the result variable 
} 

Ecco una funzione PHP utile per farlo per voi (FIXED):

function unchunk_string ($str) { 

    // A string to hold the result 
    $result = ''; 

    // Split input by CRLF 
    $parts = explode("\r\n", $str); 

    // These vars track the current chunk 
    $chunkLen = 0; 
    $thisChunk = ''; 

    // Loop the data 
    while (($part = array_shift($parts)) !== NULL) { 
    if ($chunkLen) { 
     // Add the data to the string 
     // Don't forget, the data might contain a literal CRLF 
     $thisChunk .= $part."\r\n"; 
     if (strlen($thisChunk) == $chunkLen) { 
     // Chunk is complete 
     $result .= $thisChunk; 
     $chunkLen = 0; 
     $thisChunk = ''; 
     } else if (strlen($thisChunk) == $chunkLen + 2) { 
     // Chunk is complete, remove trailing CRLF 
     $result .= substr($thisChunk, 0, -2); 
     $chunkLen = 0; 
     $thisChunk = ''; 
     } else if (strlen($thisChunk) > $chunkLen) { 
     // Data is malformed 
     return FALSE; 
     } 
    } else { 
     // If we are not in a chunk, get length of the new one 
     if ($part === '') continue; 
     if (!$chunkLen = hexdec($part)) break; 
    } 
    } 

    // Return the decoded data of FALSE if it is incomplete 
    return ($chunkLen) ? FALSE : $result; 

} 
+0

Eccellente, funziona esattamente come previsto. Questa è una comoda funzione PHP, lo sto cercando da un po 'ora. Molte grazie! – user1309276

+0

@ user1309276 Ho aggiornato la funzione di cui sopra, ha avuto un errore che circonda il comportamento quando la stringa contiene un CRLF letterale. Questo problema è stato risolto e questo ha anche fornito un migliore rilevamento di stringhe malformate. – DaveRandom

+0

Grazie ancora! Per chiunque abbia ancora problemi, dopo aver chiamato unchunk_string tutto quello che devo fare è rimuovere i primi 10 byte usando: $ data = gzinflate (substr ($ data, 10)); – user1309276

0

Per decodificare usare una stringa gzinflate, Zend_Http_Client lib aiuterà a fare questo tipo di attività comuni, la sua wasy da usare, fare riferimento Zend_Http_Response code se avete bisogno di farlo da soli

+0

Purtroppo ho già provato il metodo utilizzato da lib, ma contiene del codice che potrebbe essere necessario in futuro, grazie! – user1309276

Problemi correlati