private String setConnectionHeaders(URL url, String host, String method)
throws IOException {
HeaderManager headers = getHeaderManager();
AuthManager auth = getAuthManager();
StringBuffer hbuf = new StringBuffer();
// Allow Headers to override Host setting
hbuf.append("Host").append(COLON_SPACE).append(host).append(NEWLINE);//$NON-NLS-1$
setInt(0xA00b); //Host
setString(host);
if(headers != null) {
CollectionProperty coll = headers.getHeaders();
PropertyIterator i = coll.iterator();
while(i.hasNext()) {
Header header = (Header)i.next().getObjectValue();
String n = header.getName();
String v = header.getValue();
hbuf.append(n).append(COLON_SPACE).append(v).append(NEWLINE);
int hc = translateHeader(n);
if(hc > 0) {
setInt(hc+AJP_HEADER_BASE);
} else {
setString(n);
}
setString(v);
}
}
if(method.equals(POST)) {
int cl = -1;
String fn = getFilename();
if(fn != null && fn.trim().length() > 0) {
File input = new File(fn);
cl = (int)input.length();
body = new FileInputStream(input);
setString(HEADER_CONTENT_DISPOSITION);
setString("form-data; name=\""+encode(getFileField())+
"\"; filename=\"" + encode(fn) +"\""); //$NON-NLS-1$ //$NON-NLS-2$
String mt = getMimetype();
hbuf.append(HEADER_CONTENT_TYPE).append(COLON_SPACE).append(mt).append(NEWLINE);
setInt(0xA007); // content-type
setString(mt);
} else {
hbuf.append(HEADER_CONTENT_TYPE).append(COLON_SPACE).append(APPLICATION_X_WWW_FORM_URLENCODED).append(NEWLINE);
setInt(0xA007); // content-type
setString(APPLICATION_X_WWW_FORM_URLENCODED);
StringBuffer sb = new StringBuffer();
boolean first = true;
PropertyIterator args = getArguments().iterator();
while(args.hasNext()) {
JMeterProperty arg = args.next();
if(first) {
first = false;
} else {
sb.append('&');
}
sb.append(arg.getName()).append('=').append(arg.getStringValue());
}
byte [] sbody = sb.toString().getBytes(); //FIXME - encoding
cl = sbody.length;
body = new ByteArrayInputStream(sbody);
}
hbuf.append(HEADER_CONTENT_LENGTH).append(COLON_SPACE).append(String.valueOf(cl)).append(NEWLINE);
setInt(0xA008); // Content-length
setString(String.valueOf(cl));
}
if(auth != null) {
String authHeader = auth.getAuthHeaderForURL(url);
if(authHeader != null) {
setInt(0xA005); // Authorization
setString(authHeader);
hbuf.append(HEADER_AUTHORIZATION).append(COLON_SPACE).append(authHeader).append(NEWLINE);
}