You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "Dmitry (JIRA)" <ji...@apache.org> on 2016/04/05 15:33:25 UTC

[jira] [Commented] (HTTPCORE-316) HeaderGroup clone removes headers from original

    [ https://issues.apache.org/jira/browse/HTTPCORE-316?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15226248#comment-15226248 ] 

Dmitry commented on HTTPCORE-316:
---------------------------------

Guys, I know this is an old ticket but I would like start here before raising a new one straight away.

So Markus says "As clone does a shallow copy the List is not cloned (that is correct)".
My question is why exactly is this a correct behaviour? I would expect HeaderGroup.clone() to create a copy that can be manipulated independently of the original. But in reality both copies will be backed by the same list.

This affect HttpClient library later as when you try to clone() HttpRequest it gives you another request effectively using the same HeaderGroup so whatever you add to one, will be visible in another.

The whole thing really looks like a bug to me to be honest but according to the statement above it is the correct behaviour. Could you please explain?

Thanks

> HeaderGroup clone removes headers from original
> -----------------------------------------------
>
>                 Key: HTTPCORE-316
>                 URL: https://issues.apache.org/jira/browse/HTTPCORE-316
>             Project: HttpComponents HttpCore
>          Issue Type: Bug
>          Components: HttpCore
>    Affects Versions: 4.2.2
>         Environment: All platforms
>            Reporter: Markus Thies
>              Labels: HeaderGroup, patch
>             Fix For: 4.2.3
>
>   Original Estimate: 2h
>  Remaining Estimate: 2h
>
> The class org.apache.http.message.HeaderGroup  provides a method clone().
> If clone is called the original object's headers are removed.
> The root cause of this is, that the class HeaderGroup has only one attribute (headers) which is a List.
> As clone does a shallow copy the List is not cloned (that is correct).
> But within the method clone(), the headers of the newly created clone are removed (by calling clear()) but they actually also clear the headers of the original object (since it is not a copy).
> So this leads to very tricky problems in code where the headers are essential to be available in the clone and in the original object.
> Original code:
>     public Object clone() throws CloneNotSupportedException {
>         HeaderGroup clone = (HeaderGroup) super.clone();
>         clone.headers.clear();
>         clone.headers.addAll(this.headers);
>         return clone;
>     }
> Corrected code:
>     public Object clone() throws CloneNotSupportedException {
>         HeaderGroup clone = (HeaderGroup) super.clone();
>         //BUG: would also clear the headers original 
>         //clone.headers.clear();
>         //clone.headers.addAll(this.headers);
>         return clone;
>     }



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org