我在iOS上,我正在尝试发送HTTP POST请求,通过充当IPP服务器的Mac在打印机上创建打印作业。我可以使用airprint正确打印,我现在正在尝试进入低级别,因为我需要将原始数据发送到打印机。
关于我的代码:
一切都是硬编码的。我还在试图找出出错的地方,所以我只是处于开发/测试阶段。 第一部分是关于IPP打印作业请求的二进制结构的定义(根据RFC 2910规范,它应该是正确的。)
char data[239] = {0x01, 0x01, // IPP version
0x00, 0x04, // Print-job request
0x00, 0x00, 0x00, 0x50, // Arbitrary request ID
0x01, // Attribute group
// ATT 1
0x47, // charset value-tag
0x00, 0x12, // name-length
'a','t','t','r','i','b','u','t','e','s','-','c','h','a','r','s','e','t', // name
0x00, 0x05, // value-length
'u','t','f','-','8', // value
// ATT 2
0x48, // natural-language value-tag
0x00, 0x1B, // name-length
'a','t','t','r','i','b','u','t','e','s','-','n','a','t','u','r','a','l','-','l','a','n','g','u','a','g','e', // name
0x00, 0x05, // value-length
'e','n','-','u','s', // value
// ATT 3
0x45, // uri type value-tag
0x00, 0x0B, // name-length
'p','r','i','n','t','e','r','-','u','r','i', // name
0x00, 0x47, // value-length
'i','p','p',':','/','/','A','n','d','r','e','a','s','-','M','a','c','B','o','o','k','-','P','r','o','-','2','.','l','o','c','a','l','.',':','6','3','1','/','p','r','i','n','t','e','r','s','/','H','P','_','D','e','s','k','j','e','t','_','F','4','5','0','0','_','s','e','r','i','e','s', // value
// ATT 4
0x42, // requesting user id value-tag
0x00, 0x14, // name-length
'r','e','q','u','e','s','t','i','n','g','-','u','s','e','r','-','n','a','m','e', // name
0x00, 0x05, // value-length
'g','u','e','s','t', // value
// ATT 5
0x49, // document format value-tag
0x00, 0x0f, // document format
'd','o','c','u','m','e','n','t','-','f','o','r','m','a','t',
0x18, // TODO
'a','p','p','l','i','c','a','t','i','o','n','/','o','c','t','e','t','-','s','t','r','e','a','m',
0x03, // end of attributes
't','e','s','t'}; // data
NSMutableData *printJob = [NSMutableData data];
[printJob appendBytes:data length:sizeof(data)];
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:@"%d", [printJob length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://192.168.1.23:631/printers/HP_Deskjet_F4500_series"]];
[request setHTTPMethod:@"POST"];
[request setValue:requestDataLengthString forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/ipp" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"Andreas-MacBook-Pro-2.local" forHTTPHeaderField:@"Host"];
[request setValue:@"CUPS/1.5.0" forHTTPHeaderField:@"User-Agent"];
[request setValue:@"100-continue" forHTTPHeaderField:@"Expect"];
[request setHTTPBody:printJob];
[[NSURLConnection connectionWithRequest:request delegate:self] start];
每当我运行此代码时,10秒后我收到HTTP 400错误的请求响应。
奇怪的是,每当airprint打印将打印作业发送到ipp服务器时,HTTP请求看起来与使用数据包分析器捕获的HTTP请求完全相同(并且可以正常工作)。
答案 0 :(得分:2)
您必须为打印作业:
使用正确的ipp操作代码0x00, 0x04, // =Validate-job request
0x00, 0x02, // Print-job request
您应该使用文档格式text/plain
而不是octet-stream
。还要确保打印机支持数据格式,或者至少确保CUPS配置了适当的转换器(看起来您通过CUPS与HP打印机通信)。
不要担心user-agent
,这并不重要。而是要关心你的http协议处理!通过发送Expect: 100-continue
,您可以使CUPS期望分组的http。不要发送Expect-Header
,一切都应该没问题。
确保正确计算ipp-request加数据长度的长度并将其放入content-length-header。
答案 1 :(得分:1)
以下为我打印。我的打印机在端口80和631上都接受IPP。
// See Print-Job Request sample on page 32
// in Appendix A: Protocol Examples
// of RFC 2910 IPP/1.1 Encoding and Transport
// https://tools.ietf.org/html/rfc2910
char data[239] = {0x01, 0x01, // IPP version
0x00, 0x02, // Print-job request
0x00, 0x00, 0x00, 0x50, // Arbitrary request ID
0x01, // Attribute group
// ATT 1
0x47, // charset value-tag
0x00, 0x12, // name-length
'a','t','t','r','i','b','u','t','e','s','-','c','h','a','r','s','e','t', // name
0x00, 0x05, // value-length
'u','t','f','-','8', // value
// ATT 2
0x48, // natural-language value-tag
0x00, 0x1B, // name-length
'a','t','t','r','i','b','u','t','e','s','-','n','a','t','u','r','a','l','-','l','a','n','g','u','a','g','e', // name
0x00, 0x02, // value-length
'e','n', // value
// ATT 3
0x45, // uri type value-tag
0x00, 0x0B, // name-length
'p','r','i','n','t','e','r','-','u','r','i', // name
0x00, 0x1A, // value-length
'i','p','p',':','/','/','1','7','2','.','0','2','0','.','0','1','0','.','0','0','8',':','0','8','0','/', // value
// ATT 4
0x42, // requesting user id value-tag
0x00, 0x14, // name-length
'r','e','q','u','e','s','t','i','n','g','-','u','s','e','r','-','n','a','m','e', // name
0x00, 0x09, // value-length
'a','s','s','o','c','i','a','t','e', // value
// ATT 5
0x49, // document format value-tag
0x00, 0x0f, // document format
'd','o','c','u','m','e','n','t','-','f','o','r','m','a','t',
0x00, 0x0f, // value-length
'a','p','p','l','i','c','a','t','i','o','n','/','p','d','f',
0x02, // start job-attributes tag
0x21, // integer type value-tag
0x00, 0x06, // name-length
'c','o','p','i','e','s',
0x00, 0x04, // value-length
0x00, 0x00, 0x00, 0x01, // 1 copy
// could add 'sides' with 'two-sided-long-edge' for double sided prints (see RFC)
0x03 // end of attributes
}; // follow with pdf file data
NSMutableData *printJob = [NSMutableData dataWithBytes:data length:sizeof(data)];
NSString *pdfFilePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@"pdf"];
NSData* pdfData = [NSData dataWithContentsOfFile:pdfFilePath options:NSDataReadingUncached error:NULL];
[printJob appendData:pdfData];
NSString* requestDataLengthString = [[NSString alloc] initWithFormat:@"%ld", [printJob length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://172.020.010.008:080/"]];
[request setHTTPMethod:@"POST"];
[request setValue:requestDataLengthString forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/ipp" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:printJob];
NSError *error;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
我建议如果您需要帮助,请参阅使用任何IPP调用格式的实用示例,使用ipptool生成请求并将其捕获到文件中。我无法让Burp代理对其进行拦截(即使使用http_proxy环境变量也是如此),但是此节点脚本确实可以解决问题:
const http = require('http');
const fs = require('fs');
http.createServer(function (req, res) {
console.log('Request Received');
var body = '';
res.writeHead(200, {
'Context-Type': 'text/plain',
'Access-Control-Allow-Origin': '*'
});
req.on('data', function (chunk) {
body += chunk;
});
req.on('end', function() {
fs.writeFileSync('file.txt', body, 'utf8');
res.end('{"msg": "OK"}');
})
}).listen(8080, '127.0.0.1');
console.log('Server running at http://127.0.0.1:8080/')
然后您可以呼叫节点服务器:
ipptool -vt -f sample.pdf ipp://127.0.0.1:8080/ print-job.test
并检查请求:
hexdump -C -n256 file.txt