PHP Classes

Multipart: Missing \r\n after boundry

Recommend this page to a friend!

      cHTTP  >  All threads  >  Multipart: Missing \r\n after boundry  >  (Un) Subscribe thread alerts  
Subject:Multipart: Missing \r\n after boundry
Summary:Some servers wouldn't accept the data - updated class below
Messages:1
Author:Werner Avenant
Date:2005-08-04 14:41:38
 

  1. Multipart: Missing \r\n after boundry   Reply   Report abuse  
Picture of Werner Avenant Werner Avenant - 2005-08-04 14:41:38
Hey Guys

Here is a new version of the class that fixes the newline problem, and also 1 or 2 small other changed. See Updates comment

/************************************************************
GET, POSTS and Multipat Posts ( Upload files and etc..)
CREATED BY: Tiago Serafim
DATE : 30/08/2003
EMAIL : TIAGO at HEX dot COM dot BR

This class Extends cHTTP Class
http://www.phpclasses.org/browse.html/package/1119.html
************************************************************/
/*
Date Update

2005/08/05 Werner Avenant ([email protected])
There were newlines missing directly after the content-disposition for text fields.
Changed the User-Agent to something more "official" as certain sites block stange user agents
Moved Cookie and Referer to be above Content-Type. Seems like some servers want the content parts
directly after you specifiy the content header.


*/
require "cHTTP.php";

class cHTTPMultipart extends cHTTP {

var $boundary;

var $multiPartFields = "";

function cHTTPMultipart() {

$this->boundary = "---------------------------" . md5(time());


}

function addMultipart($field) {

$this->multiPartFields.= $field;

}

function addMultipartField($sName, $sValue) {

$temp = "\r\nContent-Disposition: form-data; name=\"$sName\"";
$temp .= "\r\n\r\n";
$temp .= $sValue;
$temp .= "\r\n";
$temp .= "--" . $this->getBoundary();

$this->addMultipart($temp);

}

function addMultipartFile($sName, $sFileName) {

$fileExt = substr($sFileName, strrpos($sFileName, ".")+1);
$theFile = implode(file($sFileName), "");

$temp = "\r\nContent-Disposition: form-data; name=\"$sName\"; filename=\"$sFileName\"";
$temp .= "\r\n";
$temp .= "Content-Type: " . $this->getContentType($fileExt) . "";
$temp .= "\r\n\r\n";
$temp .= $theFile;
$temp .= "\r\n";
$temp .= "--" . $this->getBoundary(); // werner: took out "\r\n" because it is added at beggining of Conent-disposiion

$this->addMultipart($temp);

}

function postPageMultipart($sURL) {
/*
IMPORTANT - multipart/form-data Especification

RFC: Nebel, E. and L. Masinter, "Form-based File Upload in HTML", RFC 1867, November 1995.

URL: http://www.faqs.org/rfcs/rfc1867.html

*/

$sInfo = $this->parseRequest($sURL);
$request = $sInfo['request'];
$host = $sInfo['host'];
$port = $sInfo['port'];

$httpHeader = "POST $request HTTP/1.0\r\n";
$httpHeader .= "Host: $host\r\n";
$httpHeader .= "Connection: Close\r\n";
$httpHeader .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4)\r\n"; // Werner: Changed this to something "official" as some webapps checks for this

$httpHeader .= "Referer: " . $this->referer . "\r\n";

$httpHeader .= "Cookie: " . $this->theCookies . "\r\n";


$httpHeader .= "Content-Type: multipart/form-data; boundary=" . $this->getBoundary() . "\r\n";;

$httpHeader .= "Content-length: " . strlen($this->multiPartFields) . "\r\n";

$httpHeader .= "\r\n";

$httpHeader .= "--" . $this->getBoundary();

//$httpHeader .= "\r\n"; -- werner: took this out because newlines are added at beggining of ContentBoundry

$httpHeader .= $this->multiPartFields;

$httpHeader .= "--\r\n\r\n";

echo (nl2br($httpHeader));

$this->theData = $this->downloadData($host, $port, $httpHeader); // envia os dados para o servidor


}




function getBoundary() {
return $this->boundary;;
}

function getContentType($ext) {

switch($ext) {

case "txt": return "text/plain";
case "rar": return "application/rar";
case "zip": return "application/zip";
case "exe": return "application/octet-stream";
case "jpg":
case "jpge": return "image/jpeg";
case "gif": return "image/jpeg";
case "htm":
case "html": return "text/html";
case "css": return "text/css";
case "pdf": return "application/pdf";
case "ppt": return "application/ms-powerpoint";
case "doc": return "application/msword";
case "xls": return "application/ms-excel";

default: return "application/unkown";

}

}




} // class


?>