LINUX.ORG.RU

Непонятный HTTP заголовок при заливке файла на multiupload.com


0

0

Задача: создать php/curl скипт для афтоматической заливки файлов на multiupload.com.

Обычно юзаю firefox + LiveHTTP Add-on для того что бы посмотреть что передаётся через POST и делаю аналогично. На этот раз возник вопрос: что значит когда в POST методе при Content-type: multipart/form-data используется конструкция типа `Content-Disposition: form-data; name=«file_0»; filename=«test»`? Т.е. непонятно, у этого поля 2 имени что ли? Т.е., имеем HTTP заголовок такого вида (некоторые поля удалены для краткости):

http://www10.multiupload.com/upload/?UPLOAD_IDENTIFIER=951268658335216



POST /upload/?UPLOAD_IDENTIFIER=951268658335216 HTTP/1.1

Host: www10.multiupload.com

User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.4) Gecko/20091220 Gentoo Firefox/3.5.4

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 300

Connection: keep-alive

Referer: http://www.multiupload.com/

Cookie: u=SOME_HASH_HERE_CONSISTING_OF_CAPITAL_LETTERS_AND_DIGITS

Content-Type: multipart/form-data; boundary=---------------------------124818502386333781292851266

Content-Length: 8228

-----------------------------124818502386333781292851266

Content-Disposition: form-data; name="UPLOAD_IDENTIFIER"



951268658335216

-----------------------------124818502386333781292851266

Content-Disposition: form-data; name="u"



*HASH_SAME_AS_COOKIE_HERE*

-----------------------------124818502386333781292851266

Content-Disposition: form-data; name="file_0"; filename="test"

Content-Type: application/octet-stream



test file for uploading


-----------------------------124818502386333781292851266

Content-Disposition: form-data; name="description_0"





-----------------------------124818502386333781292851266

Content-Disposition: form-data; name="fetchfield0"


........................

Т.е. вопрос касается 3-его сверху поля (после полей name=«UPLOAD_IDENTIFIER» и name=«u»). Обратите внимание: name=«file_0»; filename=«test». Что это значит и как закодить на php/curl? Пока что написал вот что, но оно не работает (файл не заливается):


<?php

class Curl {
	public $cookies = array();
	public $ch;
	
	public function __construct() {
		$this->ch = curl_init();
		curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($this->ch, CURLOPT_HEADER, 1);
		curl_setopt($this->ch, CURLOPT_VERBOSE, 1);
	}
	public function close() {
		curl_close($this->ch);
	}
	public function cookie_string() {
		$cookies_r = array();
		foreach ($this->cookies as $cookie_name => $cookie_val) {
			$cookies_r[] = urlencode($cookie_name)."=".urlencode($cookie_val);
		}
		$cookies_r = join('; ', $cookies_r);
		return $cookies_r;
	}
	public function request($url, $post = false, $postvars = array()) {
		curl_setopt($this->ch, CURLOPT_URL, $url);
		curl_setopt($this->ch, CURLOPT_COOKIE, Curl::cookie_string());
		if ($post) {
			curl_setopt($this->ch, CURLOPT_POST, 1);
			curl_setopt($this->ch, CURLOPT_POSTFIELDS, $postvars);
		}
		$serv_resp = curl_exec($this->ch);
		
		$h_end = strpos($serv_resp, "\r\n\r\n");
		$header = substr($serv_resp, 0, $h_end);
		$content = substr($serv_resp, $h_end + 4);
		
		if (preg_match_all('/Set-Cookie:\s*([^=]+)=([^;]*)/is', $header, $matches)) {
			foreach ($matches[1] as $n => $cookie_name) {
				$cookie_value = $matches[2][$n];
				$this->cookies[$cookie_name] = $cookie_value;
			}
		}
		
		return $content;
	}
}

$ch = new Curl();
$html = $ch->request("http://multiupload.com/login");
$html = $ch->request(
	"http://multiupload.com/login",
	true,
	"username=".urlencode($config['multiupload.com_login'])."&password=".urlencode($config['multiupload.com_password'])
);
$html = $ch->request("http://multiupload.com");

if (!preg_match_all('/(<form[^>]+>)/is', $html, $matches)) die("No forms on multiupload.com page\n");
$upload_identifier = '';
foreach ($matches[1] as $form_tag) {
	if (preg_match('/UPLOAD_IDENTIFIER=(\d+)/is', $form_tag, $matches)) {
		$upload_identifier = $matches[1];
		break;
	}
}
if ($upload_identifier == '') die("No UPLOAD_IDENTIFIER\n");

$html = $ch->request(
	"http://multiupload.com/upload/?UPLOAD_IDENTIFIER=$upload_identifier",
	true,
	array(
		"UPLOAD_IDENTIFIER"				=>			$upload_identifier,
		"u"								=>			urlencode($ch->cookies['u']),
// 		"file_0;filename=\"test\""						=>			"@/home/par/projects/new_nzbs/uploads/test",
		
		"file_0"						=>			"@/home/par/projects/new_nzbs/uploads/test",
		"description_0"					=>			"",
		"fetchfield0"					=>			"",
		"fetchdesc0"					=>			"",
		
		"file_1"						=>			"",
		"description_1"					=>			"",
		
		"file_2"						=>			"",
		"description_2"					=>			"",
		
		"file_3"						=>			"",
		"description_3"					=>			"",
		
		"file_4"						=>			"",
		"description_4"					=>			"",
		
		"file_5"						=>			"",
		"description_5"					=>			"",
		
		"file_6"						=>			"",
		"description_6"					=>			"",
		
		"file_7"						=>			"",
		"description_7"					=>			"",
		
		"file_8"						=>			"",
		"description_8"					=>			"",
		
		"file_9"						=>			"",
		"description_9"					=>			"",
		
		"fetchfield1"					=>			"http://",
		"fetchdesc1"					=>			"",
		
		"fetchfield2"					=>			"http://",
		"fetchdesc2"					=>			"",
		
		"fetchfield3"					=>			"http://",
		"fetchdesc3"					=>			"",
		
		"fetchfield4"					=>			"http://",
		"fetchdesc4"					=>			"",
		
		"fetchfield5"					=>			"http://",
		"fetchdesc5"					=>			"",
		
		"fetchfield6"					=>			"http://",
		"fetchdesc6"					=>			"",
		
		"fetchfield7"					=>			"http://",
		"fetchdesc7"					=>			"",
		
		"fetchfield8"					=>			"http://",
		"fetchdesc8"					=>			"",
		
		"fetchfield9"					=>			"http://",
		"fetchdesc9"					=>			"",
		
		"service_1"						=>			"1",
		"service_7"						=>			"1",
		"service_9"						=>			"1",
		
		"fromemail"						=>			"",
		"toemail"						=>			"",
		
		"rsaccount"						=>			"C",
		
		"username_5"					=>			"",
		"password_5"					=>			"",
		"remember_5"					=>			"1",
		
		"username_1"					=>			"megaupload.com username",
		"password_1"					=>			"megaupload.com password",
		"remember_1"					=>			"1",
		
		"username_7"					=>			"depositfiles username",
		"password_7"					=>			"depositfiles password",
		"remember_7"					=>			"1",
		
		"username_9"					=>			"hotfile username",
		"password_9"					=>			"hotfile password",
		"remember_9"					=>			"1",
		
		"username_6"					=>			"",
		"password_6"					=>			"",
		"remember_6"					=>			"1",
		
		"username_10"					=>			"",
		"password_10"					=>			"",
		"remember_10"					=>			"1",
	)
);

echo "|$html|\n";

$ch->close();

?>

Если в массив полей написать что то типа «file_0; filename=\„test\“» => «@/path/to/file/for/upload», то curl при curl_setopt($ch, CURLOPT_VERBOSE, 1) вообще пишет что то типа что не смог скомпоновать post request и естественно так же ничего не заливается.

PS. Искал ответ в RFC, но не нашёл. Прошу знающих людей подсказать.

#!/usr/bin/perl

use strict;

use LWP::UserAgent;
use HTTP::Request::Common;
use HTTP::Headers;

my $ua = LWP::UserAgent->new(keep_alive => 1);
my $request = GET 'http://multiupload.com/';
my $response = $ua->request($request);

my ($url) = $response->content =~ m!action="(http://www\d*\.multiupload\.com/upload/\?UPLOAD_IDENTIFIER=\d+)"!;
my ($id)  = $response->content =~ /name="UPLOAD_IDENTIFIER" value="(\d+)"/;
my ($u)   = $response->content =~ /name="u" value="([^"]+)"/;
print "** id=$id || u=$u\n** url=$url\n";

$request = POST $url, Content_Type => 'form-data', Content => [
        UPLOAD_IDENTIFIER => $id, u => $u,
        file_0 => [ '/etc/services' ], description_0 => 'test',
];
$response = $ua->request($request);
my ($dlid) = $response->content =~ /"downloadid":"([^"]+)"/;
print "\ndownload id: $dlid\n";

http://www.multiupload.com/H92YOI67L9

arsi ★★★★★ ()
Ответ на: комментарий от www_linux_org_ru

Спасибо всем отписавшимся. Проблема со скриптом, как выяснилась, вобще была с тем что POST делался не на то что в атрибуте «action» upload-формы.

par12b12 ()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.